[EMAIL PROTECTED] wrote: > I have taken the coments and think I have implemented most. My only > question is how to use the enumerator. Here is what I did, I have tried > a couple of things but was unable to figure out how to get the line > number. > > Try this in the interpreter,
l = [5,4,3,2,1] for count, i in enumerate(l): print count, i > def Xref(filename): > try: > fp = open(filename, "r") > except: > raise "Couldn't read input file \"%s\"" % filename > dict = {} > line_num=0 > for words in iter(fp.readline,""): > words = set(words.split()) > line_num = line_num+1 > for word in words: > word = word.strip(".,!?:;") > if not dict.has_key(word): > dict[word] = [] > dict[word].append(line_num) > fp.close() > keys = sorted(dict); > for key in keys: > print key," : ", dict[key] > return dict > > Marc 'BlackJack' Rintsch wrote: > >> In <[EMAIL PROTECTED]>, >> [EMAIL PROTECTED] wrote: >> >> >>> def Xref(filename): >>> try: >>> fp = open(filename, "r") >>> lines = fp.readlines() >>> fp.close() >>> except: >>> raise "Couldn't read input file \"%s\"" % filename >>> dict = {} >>> for line_num in xrange(len(lines)): >>> >> Instead of reading the file completely into a list you can iterate over >> the (open) file object and the `enumerate()` function can be used to get >> an index number for each line. >> >> >>> if lines[line_num] == "": continue >>> >> Take a look at the lines you've read and you'll see why the ``continue`` >> is never executed. >> >> >>> words = lines[line_num].split() >>> for word in words: >>> if not dict.has_key(word): >>> dict[word] = [] >>> if line_num+1 not in dict[word]: >>> dict[word].append(line_num+1) >>> >> Instead of dealing with words that appear more than once in a line you may >> use a `set()` to remove duplicates before entering the loop. >> >> Ciao, >> Marc 'BlackJack' Rintsch >> > > -- http://mail.python.org/mailman/listinfo/python-list