indexerror: list index out of range??

Dave Angel davea at davea.name
Sat Jun 29 09:44:55 EDT 2013


On 06/28/2013 11:35 PM, Titiksha wrote:
> On Friday, June 28, 2013 8:20:28 PM UTC-5, Titiksha wrote:
>>
          <SNIP double-spaced nonsense>
>>
>> m=['631138', '601034', '2834', '2908', '64808']
>>
          <SNIP more double-spaced nonsense>
>>
>>
>>
>> ['LAKEFLD  3227,631138\n', 'NOBLES   3013,601034\n']
>>
>>

Since you're using the arrogant and buggy GoogleGroups, this 
http://wiki.python.org/moin/GoogleGroupsPython.

>>
>>
>> I see the line,a being correct but print (i) does not show up after 2. and index error comes up. I am too confused now. Please guide.
>>
>> Thanks in advance.
>
> Thanks for helping out!
> Dave you mentioned about false matches in case of string in m is substring of line. How do I manage that issue? Is there any other method I should look into?

Suppose one of the items in m were '1234', and suppose one of the lines 
in the file be
'HARMONY 12,441234913'

Your current logic would consider it a match, and I'm assuming that 
would be a false match.

To fix that, you need to parse the line from the file, and separate it 
into fields, one of which needs to exactly match 1234.

You call it a csv file, and if it were, you could just use the csv 
module.  But there's no comma between LAKEFLD and 3227, so the line 
would be considered to have two fields.  If that's correct, then you're 
golden.  Just use csv to get the fields, and compare m[i] == field[1] 
rather than  m[i] in line.


>
> What I am looking to do is..I have a list of m which I need to map in the same sequence to the ALL_BUSES_FINAL file and get the entire line which has the string in m.I want to iterate through all the lines in ALL_BUSES_FINAL to match the all strings in m.
>
Any way I can interpret those sentences, it contradicts itself.  Could 
you just post the complete assignment, without paraphrasing?

Taking an individual phrase of what you said:  "same sequence" implies 
you do NOT want to re-open the file multiple times. So move the open 
outside of the while loop.  Add a test and a break after incrementing i, 
since you'll quit looking once you have a match for all the items, and 
you'll know that when i reaches the len of m.

Hopefully you'll know how to get the single line out of a when you're 
done, maybe by concatenating field[0] of each line.


-- 
DaveA



More information about the Python-list mailing list