Is there a method that returns a character at a specified index?

M

Michael Loomington

Couldn't find anything here
http://www.python.org/doc/2.3.2/lib/string-methods.html

I need to cycle through lines in a file that contain words and compare it to
a string thats given, call it SSS to see if the words in the file can be
written from SSS. I am planning to grab each letter from the word one at a
time and removing it from SSS and if the letter doesn't exist then I will go
on to the next word. That's why I need a method that returns a letter at
each index. Maybe there an easier way of doing this.
 
C

Cameron Laird

Couldn't find anything here
http://www.python.org/doc/2.3.2/lib/string-methods.html

I need to cycle through lines in a file that contain words and compare it to
a string thats given, call it SSS to see if the words in the file can be
written from SSS. I am planning to grab each letter from the word one at a
time and removing it from SSS and if the letter doesn't exist then I will go
on to the next word. That's why I need a method that returns a letter at
each index. Maybe there an easier way of doing this.

I don't understand your description.

I'm certain there's an easier way.

My guess is that this is an example of what you think you want:
Python 2.3+ (#2, Aug 10 2003, 11:33:47)
[GCC 3.3.1 (Debian)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> alphabet = "abcdefghijklmnopqrstuvwxyz"
>>> alphabet[3] 'd'
>>> alphabet[14] 'o'
>>>
 
A

Alex Martelli

Michael said:
Couldn't find anything here
http://www.python.org/doc/2.3.2/lib/string-methods.html

I need to cycle through lines in a file that contain words and compare it
to a string thats given, call it SSS to see if the words in the file can
be
written from SSS. I am planning to grab each letter from the word one at
a time and removing it from SSS and if the letter doesn't exist then I
will go
on to the next word. That's why I need a method that returns a letter at
each index. Maybe there an easier way of doing this.

Absolutely -- much faster. Check out the string.maketrans function
in module string, and the .translate method of string instances. The
Python Cookbook, in discussion of recipes 3.6 and 3.7 on pp. 76-78,
has in-depth discussion and examples.


Alex
 
B

Bengt Richter

Couldn't find anything here
http://www.python.org/doc/2.3.2/lib/string-methods.html

I need to cycle through lines in a file that contain words and compare it to
a string thats given, call it SSS to see if the words in the file can be
written from SSS. I am planning to grab each letter from the word one at a
time and removing it from SSS and if the letter doesn't exist then I will go
on to the next word. That's why I need a method that returns a letter at
each index. Maybe there an easier way of doing this.
I'm not sure what you intend to do if you find that "words in the file can be
written from SSS," but perhaps you will need to know where in SSS the words are?
Perhaps you can get something useful from the snippet below, which suggests checking
for word availability in SSS by using a dict. Otherwise if SSS is big, you will be
re-scanning half of SSS each time on the average to find a word, and all of it if
the word is not there, so it will be inefficient. I split out the words in the lines
assuming spaces as delimiters, but you may need to use a regex to split away punctuation
marks as well, depending on your actual application. Obviously you could get lines from
a file with "for line in file('foo.txt'):" instead of the "for line in lines:" below,
but mind the '\n' and possible other leading and trailing white space that you get from
iterating through a file by lines.
>>> SSS = 'nuts bolts screws nails'
>>> SSS 'nuts bolts screws nails'
>>> sslist = SSS.split()
>>> sslist ['nuts', 'bolts', 'screws', 'nails']
>>> ssdict = dict([(w, (SSS.index(w), len(w))) for w in sslist])
>>> ssdict {'screws': (11, 6), 'nails': (18, 5), 'nuts': (0, 4), 'bolts': (5, 5)}
>>> lines = """\
... line 1 has nuts and bolts
... line 2 has screws and nails
... line 3 has no words from SSS
... """.splitlines()
>>> lines ['line 1 has nuts and bolts', 'line 2 has screws and nails', 'line 3 has no words from SSS']
>>> for line in lines:
... print 'Line: %r' % line
... words = line.split()
... for word in words:
... if word in ssdict:
... print ' %r is %s chars starting at %s in SSS' %(
... word, ssdict[word][1], ssdict[word][0])
...
Line: 'line 1 has nuts and bolts'
'nuts' is 4 chars starting at 0 in SSS
'bolts' is 5 chars starting at 5 in SSS
Line: 'line 2 has screws and nails'
'screws' is 6 chars starting at 11 in SSS
'nails' is 5 chars starting at 18 in SSS
Line: 'line 3 has no words from SSS'

Regards,
Bengt Richter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top