Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #49399

Re: indexerror: list index out of range??

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.kamp.net!newsfeed.kamp.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'output': 0.05; 'elements.': 0.07; 'error:': 0.07; 'explicit': 0.07; 'nested': 0.07; 'strict': 0.07; 'string': 0.09; '40,': 0.09; 'line:': 0.09; 'csv': 0.16; 'does,': 0.16; 'for,': 0.16; 'increment': 0.16; 'indexerror:': 0.16; 'operator.': 0.16; 'range.': 0.16; 'received:74.208.4.195': 0.16; 'substring': 0.16; 'wanted.': 0.16; 'elements': 0.16; 'index': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'advance.': 0.19; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'skip:" 30': 0.26; '(for': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'subject:list': 0.30; 'code': 0.31; 'getting': 0.31; 'lines': 0.31; '>>>>': 0.31; 'file': 0.32; 'another': 0.32; 'up.': 0.33; '(most': 0.33; 'guess': 0.33; 'maybe': 0.34; 'problem': 0.35; 'something': 0.35; 'but': 0.35; 'really': 0.36; 'false': 0.36; 'thanks': 0.36; 'hi,': 0.36; 'searching': 0.37; 'wrong': 0.37; 'too': 0.37; 'list': 0.37; 'being': 0.38; 'skip:o 20': 0.38; 'follows:': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'eventually': 0.60; 'range': 0.61; "you're": 0.61; "you'll": 0.62; 'show': 0.63; 'field': 0.63; 'soon': 0.63; 'reached': 0.63; 'here': 0.66; 'received:74.208': 0.68; 'below:': 0.68; 'limit': 0.70; 'satisfied': 0.81; 'confusing': 0.84; 'presumably': 0.84; '(i)': 0.91
Date Fri, 28 Jun 2013 21:58:09 -0400
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130510 Thunderbird/17.0.6
MIME-Version 1.0
To python-list@python.org
Subject Re: indexerror: list index out of range??
References <8b12635e-c3e9-49b2-b3fa-792559088821@googlegroups.com>
In-Reply-To <8b12635e-c3e9-49b2-b3fa-792559088821@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:itPCkrb/lWB7d+E3phVhv1GjZRfmJ90QjzTn0lXDt7u 9pexKIz3DWJMm5nORfTfu/nPAC9a0t2NRuKgCs+LM2R19WAjVi wMcjvMpGDGKtMk5ifbsCRl9aNf07Bj+1JAIGaPm4umnoI7F+Ng 5XbJd6NjSlhfGbGrMfxcdFFT6oz4w41X3eaVycUSYOCEOXBDkE /sFz/27kgAcBV3o1WFjNq0OSBQ7+1R6g1HHGjGuBNyPllC/DSl e496yVwPDYypIzxVCDTgRvt5OX3p1tUIILag9BYgmNfeS9oQP7 0KjGGY117sTYwq8Zy+gHEVXeCaq2kfEodeCEI3GQDkNE1QI0Yx DFgQH5mBPKsu/WTm1hMY=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3977.1372471106.3114.python-list@python.org> (permalink)
Lines 76
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1372471106 news.xs4all.nl 15902 [2001:888:2000:d::a6]:32841
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:49399

Show key headers only | View raw


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[i] 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[i] 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

indexerror: list index out of range?? Titiksha Joshi <joshi.titiksha90@gmail.com> - 2013-06-28 18:20 -0700
  Re: indexerror: list index out of range?? Chris Angelico <rosuav@gmail.com> - 2013-06-29 11:53 +1000
  Re: indexerror: list index out of range?? Dave Angel <davea@davea.name> - 2013-06-28 21:58 -0400
  Re: indexerror: list index out of range?? Titiksha <joshi.titiksha90@gmail.com> - 2013-06-28 20:35 -0700
    Re: indexerror: list index out of range?? Dave Angel <davea@davea.name> - 2013-06-29 09:44 -0400
    Re: indexerror: list index out of range?? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-06-29 15:30 +0100
    Re: indexerror: list index out of range?? Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-29 16:52 +0100
    Re: indexerror: list index out of range?? Robert Kern <robert.kern@gmail.com> - 2013-07-01 09:45 +0100
      Re: indexerror: list index out of range?? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-01 12:30 +0000

csiph-web