How do you do arrays
Thomas Bunce
tbunce at mac.com
Tue Feb 1 15:18:15 EST 2005
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