indexerror: list index out of range??

Discussion in 'Python' started by Titiksha Joshi, Jun 29, 2013.

  1. Hi,
    I am working on the following code but am getting the error: list index out of range. I surfed through the group but somehow I am not able to fix my error.Please guide.Structure is given below:
    m is a list of 5 elements. I have to match elements of m from fields in file ALL_BUSES_FINAL.cvs.
    m=['631138', '601034', '2834', '2908', '64808']

    i=0
    while i<len(m):
    print(i)
    my_file = open("ALL_BUSES_FINAL.csv", "r+")
    for line in my_file:
    if m in line:
    print (line)
    a.append(line)
    i+=1
    print(a)
    my_file.close()


    The output is as follows:
    0
    LAKEFLD 3227,631138

    ['LAKEFLD 3227,631138\n']

    1
    NOBLES 3013,601034

    ['LAKEFLD 3227,631138\n', 'NOBLES 3013,601034\n']

    2
    GR_ISLD I,2834

    ['LAKEFLD 3227,631138\n', 'NOBLES 3013,601034\n', 'GR_ISLD I,2834\n']

    FTTHOMP 928,2908

    ['LAKEFLD 3227,631138\n', 'NOBLES 3013,601034\n', 'GR_ISLD I,2834\n', 'FTTHOMP 928,2908\n']

    VICTRYH 15,64808

    ['LAKEFLD 3227,631138\n', 'NOBLES 3013,601034\n', 'GR_ISLD I,2834\n', 'FTTHOMP 928,2908\n', 'VICTRYH 15,64808\n']
    Traceback (most recent call last):
    File "C:\Users\TJ\dist_tracking.py", line 40, in <module>
    if m in line:
    IndexError: list index out of range
    >>>


    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.
    Titiksha Joshi, Jun 29, 2013
    #1
    1. Advertising

  2. On Sat, Jun 29, 2013 at 11:20 AM, Titiksha Joshi
    <> wrote:
    > Hi,
    > I am working on the following code but am getting the error: list index out of range. I surfed through the group but somehow I am not able to fix my error.Please guide.Structure is given below:
    > m is a list of 5 elements. I have to match elements of m from fields in file ALL_BUSES_FINAL.cvs.
    > m=['631138', '601034', '2834', '2908', '64808']
    >
    > i=0
    > while i<len(m):
    > print(i)
    > my_file = open("ALL_BUSES_FINAL.csv", "r+")
    > for line in my_file:
    > if m in line:
    > print (line)
    > a.append(line)
    > i+=1
    > print(a)
    > my_file.close()


    What this does is open the file once for every string in 'm', and read
    through it. But you increment i for every line in the file, that's why
    you're having problems.

    I think you probably want to open the file once and iterate over it,
    but you'll need to figure out what you're trying to do before you can
    figure out how to do it in Python :)

    Also, be up-front about homework assignments. Honesty won't hurt you
    (since we can all tell anyway), and it means we know you're trying to
    learn, not to cheat. And yes, there are a discouraging number of
    people who do try to cheat, so setting yourself apart from them is
    well worth it. :)

    Chris Angelico
    Chris Angelico, Jun 29, 2013
    #2
    1. Advertising

  3. Titiksha Joshi

    Dave Angel Guest

    On 06/28/2013 09:20 PM, Titiksha Joshi wrote:
    > Hi,
    > I am working on the following code but am getting the error: list index out of range. I surfed through the group but somehow I am not able to fix my error.Please guide.Structure is given below:
    > m is a list of 5 elements. I have to match elements of m from fields in file ALL_BUSES_FINAL.cvs.
    > m=['631138', '601034', '2834', '2908', '64808']
    >


    It's not hard to see what's wrong in the following excerpt, but it is
    hard to guess what you really wanted.

    Did you want to find all lines which match any of the strings in m? Or
    are you just satisfied to match each of the strings in m against *some*
    line of the file? Or is it something else entirely?

    You have a nested loop here (for loop inside a while loop), and you're
    confusing the limit values for the outer one.

    > i=0
    > while i<len(m):
    > print(i)
    > my_file = open("ALL_BUSES_FINAL.csv", "r+")
    > for line in my_file:
    > if m in line:
    > print (line)
    > a.append(line)
    > i+=1


    You increment i here, but do not check if it's reached the end of the m.
    So eventually it does, and crashes.

    > print(a)
    > my_file.close()
    >
    >
    > The output is as follows:
    > 0
    > LAKEFLD 3227,631138
    >
    > ['LAKEFLD 3227,631138\n']
    >
    > 1
    > NOBLES 3013,601034
    >
    > ['LAKEFLD 3227,631138\n', 'NOBLES 3013,601034\n']
    >
    > 2
    > GR_ISLD I,2834
    >
    > ['LAKEFLD 3227,631138\n', 'NOBLES 3013,601034\n', 'GR_ISLD I,2834\n']
    >
    > FTTHOMP 928,2908
    >
    > ['LAKEFLD 3227,631138\n', 'NOBLES 3013,601034\n', 'GR_ISLD I,2834\n', 'FTTHOMP 928,2908\n']
    >
    > VICTRYH 15,64808
    >
    > ['LAKEFLD 3227,631138\n', 'NOBLES 3013,601034\n', 'GR_ISLD I,2834\n', 'FTTHOMP 928,2908\n', 'VICTRYH 15,64808\n']
    > Traceback (most recent call last):
    > File "C:\Users\TJ\dist_tracking.py", line 40, in <module>
    > if m in line:
    > IndexError: list index out of range
    >>>>

    >
    > 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.
    >


    Another big problem will arise as soon as you have a string in m which
    is a strict substring of a value in the line. You'll get false matches.
    So presumably you really want to use csv logic, and have explicit
    fields that you're searching for, rather than using 'in' operator. And
    at that point, maybe you just want to check field #2 of each line.


    --
    DaveA
    Dave Angel, Jun 29, 2013
    #3
  4. Titiksha Joshi

    Titiksha Guest

    On Friday, June 28, 2013 8:20:28 PM UTC-5, Titiksha wrote:
    > Hi,
    >
    > I am working on the following code but am getting the error: list index out of range. I surfed through the group but somehow I am not able to fix my error.Please guide.Structure is given below:
    >
    > m is a list of 5 elements. I have to match elements of m from fields in file ALL_BUSES_FINAL.cvs.
    >
    > m=['631138', '601034', '2834', '2908', '64808']
    >
    >
    >
    > i=0
    >
    > while i<len(m):
    >
    > print(i)
    >
    > my_file = open("ALL_BUSES_FINAL.csv", "r+")
    >
    > for line in my_file:
    >
    > if m in line:
    >
    > print (line)
    >
    > a.append(line)
    >
    > i+=1
    >
    > print(a)
    >
    > my_file.close()
    >
    >
    >
    >
    >
    > The output is as follows:
    >
    > 0
    >
    > LAKEFLD 3227,631138
    >
    >
    >
    > ['LAKEFLD 3227,631138\n']
    >
    >
    >
    > 1
    >
    > NOBLES 3013,601034
    >
    >
    >
    > ['LAKEFLD 3227,631138\n', 'NOBLES 3013,601034\n']
    >
    >
    >
    > 2
    >
    > GR_ISLD I,2834
    >
    >
    >
    > ['LAKEFLD 3227,631138\n', 'NOBLES 3013,601034\n', 'GR_ISLD I,2834\n']
    >
    >
    >
    > FTTHOMP 928,2908
    >
    >
    >
    > ['LAKEFLD 3227,631138\n', 'NOBLES 3013,601034\n', 'GR_ISLD I,2834\n', 'FTTHOMP 928,2908\n']
    >
    >
    >
    > VICTRYH 15,64808
    >
    >
    >
    > ['LAKEFLD 3227,631138\n', 'NOBLES 3013,601034\n', 'GR_ISLD I,2834\n', 'FTTHOMP 928,2908\n', 'VICTRYH 15,64808\n']
    >
    > Traceback (most recent call last):
    >
    > File "C:\Users\TJ\dist_tracking.py", line 40, in <module>
    >
    > if m in line:
    >
    > IndexError: list index out of range
    >
    > >>>

    >
    >
    >
    > 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?

    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.
    Titiksha, Jun 29, 2013
    #4
  5. Titiksha Joshi

    Dave Angel Guest

    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 == field[1]
    rather than m 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
    Dave Angel, Jun 29, 2013
    #5
  6. On 29/06/2013 14:44, Dave Angel wrote:
    > On 06/28/2013 11:35 PM, Titiksha wrote:
    >
    > Since you're using the arrogant and buggy GoogleGroups, this
    > http://wiki.python.org/moin/GoogleGroupsPython.
    >


    Please don't make comments like this, you'll upset the Python Mailing
    List Police.

    --
    "Steve is going for the pink ball - and for those of you who are
    watching in black and white, the pink is next to the green." Snooker
    commentator 'Whispering' Ted Lowe.

    Mark Lawrence
    Mark Lawrence, Jun 29, 2013
    #6
  7. On 29 June 2013 15:30, Mark Lawrence <> wrote:
    >
    > On 29/06/2013 14:44, Dave Angel wrote:
    >>
    >> Since you're using the arrogant and buggy GoogleGroups, this
    >> http://wiki.python.org/moin/GoogleGroupsPython.
    >>

    > Please don't make comments like this, you'll upset the Python Mailing List
    > Police.


    *doesn't understand*
    Joshua Landau, Jun 29, 2013
    #7
  8. Titiksha Joshi

    Robert Kern Guest

    On 2013-06-29 16:52, Joshua Landau wrote:
    > On 29 June 2013 15:30, Mark Lawrence <> wrote:
    >>
    >> On 29/06/2013 14:44, Dave Angel wrote:
    >>>
    >>> Since you're using the arrogant and buggy GoogleGroups, this
    >>> http://wiki.python.org/moin/GoogleGroupsPython.
    >>>

    >> Please don't make comments like this, you'll upset the Python Mailing List
    >> Police.

    >
    > *doesn't understand*


    Mark frequently makes similar comments (in content and tone) to people who come
    here using Google Groups. Presumably, he has received criticism for this (mostly
    on tone grounds, I imagine), either on or off list.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
    Robert Kern, Jul 1, 2013
    #8
  9. On Mon, 01 Jul 2013 09:45:52 +0100, Robert Kern wrote:

    > On 2013-06-29 16:52, Joshua Landau wrote:
    >> On 29 June 2013 15:30, Mark Lawrence <> wrote:
    >>>
    >>> On 29/06/2013 14:44, Dave Angel wrote:
    >>>>
    >>>> Since you're using the arrogant and buggy GoogleGroups, this
    >>>> http://wiki.python.org/moin/GoogleGroupsPython.
    >>>>
    >>> Please don't make comments like this, you'll upset the Python Mailing
    >>> List Police.

    >>
    >> *doesn't understand*

    >
    > Mark frequently makes similar comments (in content and tone) to people
    > who come here using Google Groups. Presumably, he has received criticism
    > for this (mostly on tone grounds, I imagine), either on or off list.


    That would be me, and it wasn't about Google Groups.

    After Mark was repeatedly abusive towards somebody, including telling
    this person to kill themselves, and after he chose to ignore both polite
    requests to cease flaming, and a fair warning that he would be kill-filed
    if he persisted, he persisted and so I kill-filed him. Which is a pity,
    because Mark does know Python well and he can be very helpful (although
    sometimes abrasive). I think on balance, when he's not flaming, he makes
    a positive contribution to this community.

    The kill-filing is only temporary, and I look forward to seeing his posts
    again soon. Hopefully by that time, he'll stop feeling put-out and hurt
    that I've chosen to not read his flames, and cease misrepresenting my
    actions.


    --
    Steven
    Steven D'Aprano, Jul 1, 2013
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    36
    Views:
    1,579
    MonkeeSage
    Oct 11, 2006
  2. MRAB
    Replies:
    0
    Views:
    600
  3. Costin GamenÈ›
    Replies:
    1
    Views:
    293
    Peter Otten
    Nov 9, 2010
  4. Andrew Z

    IndexError: pop index out of range

    Andrew Z, May 15, 2013, in forum: Python
    Replies:
    0
    Views:
    176
    Andrew Z
    May 15, 2013
  5. Carlos Nepomuceno

    RE: IndexError: pop index out of range

    Carlos Nepomuceno, May 15, 2013, in forum: Python
    Replies:
    0
    Views:
    130
    Carlos Nepomuceno
    May 15, 2013
Loading...

Share This Page