[Tutor] Parsing Question
Alan Gauld
alan.gauld at btinternet.com
Sun May 10 02:30:04 CEST 2009
"Michael Morrissey" <gdoghomes at gmail.com> wrote
> a text file that looks like this:
> 1 the 126 name
> 2 of 127 very
>
> Each has 2 numbers and 2 words on it. Each number is related to the word
> that comes after it. So "1" and "the" are connected (kinda like a
> dictionary), and "126" and "name" are related.
>
> Using the above text file as an input, I'm trying to make an output that
> lists all the words, one word per line, but none of the numbers. I was
> hoping to get the words listed in order of their related numbers. So, the
>
> I can open and loop over the text, I'm just not sure what commands I
> should
> use to parse each line. Can anyone give me suggestions or examples?
For this a simple split() command should give you a list of 4 items.
I suggest you store each pair in a tuple (num, word) and put the
tuples in a list.
You can then sort the list and the tuples will be in numeric order
(element 0).
Then iterate over the list printing the word (element 1 of the tuple)
In pseudo code:
words = []
for line in open(fname):
four = line.split()
words.append( (four[0],four[1]) )
words.append( (four[2],four[3]) )
for word in sorted(words):
print word[1]
You should probably be able to do the first for loop as a list
comprehension, but I can't think of how to get the split() call
embedded into it right now!
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list