How do you do arrays

wes weston wweston at att.net
Tue Feb 1 15:43:21 EST 2005


Thomas,
    If you were allowed to do what you're doing, the
list first element would be getting skipped as "index"
is always > 0. The thing is, you don't want the "index"
var at all for adding to the list; just do jMatrix.append(k).
    You can iterate over the list with
for x in jMatrix:
    print x

    Is it basic that indexes from 1 vs. 0? 'just about
completely forgotten basic.
wes

Thomas Bunce wrote:
> Tryed it and this is what I got (I did go to the web sight)
> 
> tom(h=500)$ /tmp/501/Cleanup\ At\ Startup/ptesting-128981347.87.py.command; exit
> Input the maximu number of tvalue: 114
> Traceback (most recent call last):
>   File "/Users/tom/Desktop/ptesting.py", line 20, in ?
>     iMatrix[index] = k
> IndexError: list assignment index out of range
> logout
> [Process completed]
> 
> The complete listing:
> 
> #!/usr/bin/python
> import random
> import sys
> import array
> #
> ### generates a list of numbers between 1 and target
> ### and uses 23 % of these values.
> #
> 
> iMatrix = []
> 
> tvalue = input('Input the maximu number of tvalue: ')
> majorlop1 = int tvalue * .23)
> listvalues = range(tvalue)
> 
> sep = '- '
> index = 0
> while index < majorlop1:
>    index = index + 1
>    k = random.choice(listvalues) + 1
>    iMatrix[index] = k
>    
> while index < majorlop1:
>    print '- %s %s' % (iMatrix[index], sep)
> #
> ###
> #
> I would like to set the size of the List/array independent 
> of having to intialialize it prior to use. 
> If it help I will say the bad works I am using OSX
> 
>    Thanks Tom
> In article <1107284817.848551.79000 at c13g2000cwb.googlegroups.com>,
> "Kartic" <kartic.krishnamurthy at gmail.com> wrote:
> 
> 
>>Tom,
>>
>>Before you use iMatrix[index], you have to tell python to use iMatrix
>>as an array. You will do that using iMatrix = [] *outside* the loop.
>>
>>iMatrix = []
>>while index < majorlop1: # rest of the loop statements
>>
>>Since you are new, please take a look at the Python tutorial to get you
>>started.
>>http://docs.python.org/tut/tut.html
>>
>>Thanks,
>>-Kartic




More information about the Python-list mailing list