[Tutor] dynamic arrays?
Joel Goldstick
joel.goldstick at gmail.com
Mon Sep 27 18:12:55 CEST 2010
> Hi all,
>> >> One thing I have never much liked about Python is its need for
>> >> specifically sized arrays and lack of a dynamic, array-like data
>> >> structure. For example, the following fails with a "list assignment
>> >> index out of range" error:
a=[]
i=0
for l in open("file.txt", "r"):
a[i]=l
i+=1
>> >>
> A python list and a python array are one and the same to my knowledge.
Actually I looked it up, there is an array module. I've never used
it. Lists are built in data type that act like what people mean when
they say arrays -- an ordered list of values accessible by a
positional index.
The reason you get the exception is that you are trying to put
something in a[0], and you defined a as empty, so it has no a[0] yet.
len(a) == 0 initially. When you append a value
to a, your first value is stored, in a[0], etc. Read about slices.
They're fun, and informative to understand indexing concepts in python
--
Joel Goldstick
More information about the Tutor
mailing list