basic python questions

tom tom at t0mb.net
Sat Nov 18 12:22:02 EST 2006


tom wrote:
> nateastle at gmail.com 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
>
>
>   
you could do it like this.

for count, line in enumerate(fb):
    for word in line.split():
       etc...

filehandles are iterators themselves.

dont take my words for granted though, i'm kinda new to all this too :)
>> 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 <1163829271.660193.70450 at j44g2000cwa.googlegroups.com>,
>>> nateastle at gmail.com 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
>>>     
>>>       
>>   
>>     
>
>   




More information about the Python-list mailing list