[Tutor] IndexError and appending to lists [Was: Re: Need Help on Assignment]

Tom Strickland tlinux at comcast.net
Wed Aug 24 01:41:27 CEST 2005


Danny,

Thanks for your comments and your help. I've added my comments to your 
text below. Hopefully it will be in red so you can easily identify them.

Tom

Danny Yoo wrote:

>Hi Tom,
>
>Before we continue: it looks like you're starting to learn Python.  Have
>you gone through one of the tutorials here?
>
>    http://wiki.python.org/moin/BeginnersGuide/NonProgrammers
>
>Alan Gauld's tutorial is especially nice, but all of the tutorials there
>should be useful.  If you go through any one of them, each should help
>address the issues you're running into.
>  
>
Tom's Comments:  I have one of the tutorials and I'm also reading 
Learning Python by Lutz & Ascher.

>
>Ok, let's look at the program.  I'll try to make constructive criticism
>along the way.
>
>  
>
>>input = open('/home/tom/Python/Input/SPY3.txt', 'r')
>>N=0
>>s = 'boo'
>>date =['hoo']
>>T = [0,0,0,0,0,0]               #don't know why this is necessary
>>    
>>
>
>There are an awful number of global variables here, and their names do not
>make it clear what the role of each variable is.  What is 's', and what is
>'T'?  And why does date contain the string 'hoo'?
>  
>
Tom's Comments: N is the counter which I initialize here. s is a string 
and I've intiialized it to 'boo' so that it passed the first "while" 
test and entered the loop. The "date" variable is defined just to see if 
it made any difference. I had previously not defined it, and the program 
hung up on the "date" assignment so I tried assigning it a value but it 
didn't help. T is the matrix consisting of several rows of six columns 
each. If I leave out this definition of T =[0,0,0,0,0,0] the program 
hangs up on the line: T[N] = s.split(',')

>Also, the original code was double-spaced; you don't need to do that.
>You're not writing a report that's graded based on filling 80 lines, are
>you?  *grin*
>  
>

Tom's Comments: The original code was written using Eric. In going from 
Eric to Word to Thunderbird e-mail the double space crept in. It's not 
in the original. Also, the line numbers were lost along the way.

>
>
>  
>
>>open = close = hi = lo = vol = [1.0]  #is this necessary?
>>    
>>
>
>This looks highly suspicious.
>  
>

Tom's Comments: I just put this in to see if it would help. It didn't. I 
thought it might since Python didn't like for me to use T without first 
assigning it some values.

>In Python, names are just references to things.  That means that all of
>these names --- open, close, hi, low, vol --- are just aliases for the
>same list thing.  For example:
>
>######
>  
>
>>>>maui = hawaiian = ['pizza']
>>>>hawaiian[0] = 'ham and pineapple pizza'
>>>>hawaiian
>>>>        
>>>>
>['ham and pineapple pizza']
>  
>
>>>>maui
>>>>        
>>>>
>['ham and pineapple pizza']
>######
>
>Notice that because 'maui' and 'hawaiian' are refering to the same list,
>when we change the list, we see that reflected here in both 'maui' and
>'hawaiian'.
>  
>

Tom's Comments: Good point! I didn't realize this.

>
>
>  
>
>>while s:
>>    s = input.readline()
>>    if s == '':break
>>    s = s[:-2]
>>    T[N] = s.split(',')
>>    date[N] = T[N][0]
>>    open[N] = T[N][1]
>>    hi[N] = T[N][2]
>>    lo[N] = T[N][3]
>>    close[N] = T[N][4]
>>    vol[N] = T[N][5]
>>    N+=1
>>    
>>
>
>Ok, there's a problem here.
>
>One thing you may need to know is that Python's lists do not automatically
>expand as we try to enter elements into them.  For example:
>
>######
>  
>
>>>>names = ['knuth', 'morris']
>>>>names[2] = 'pratt'
>>>>        
>>>>
>Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>IndexError: list assignment index out of range
>######
>
>This breaks because the list is defined contain two elements: trying to
>assign to names[2] goes out of the list bounds, and that's an error in
>Python.  In practice, this "array out of bounds" check is often a Very
>Good Thing because index errors can be evidence of a logical program
>error.
>
>
>Anyway, this doesn't answer the problem: how do we add elements to a list?
>In Python, we can accumulate elements in a list by append()ing:
>
>######
>  
>
>>>>names = []
>>>>names.append('john')
>>>>names.append('lisa')
>>>>names.append('erik')
>>>>names.append('tina')
>>>>names
>>>>        
>>>>
>['john', 'lisa', 'erik', 'tina']
>######
>
>Does this make sense?
>  
>
Tom's Comments: Yes! So, for example, would I use the following in my 
"while" loop:

                      date.append(T[N][0])

Before the loop, date would have to be defined as date =[], is this correct?

>
>
>
>  
>



More information about the Tutor mailing list