List problems in C code ported to Python

Grant Edwards grante at visi.com
Mon Jan 17 14:50:13 EST 2005


On 2005-01-17, wittempj at hotmail.com <wittempj at hotmail.com> wrote:
>>>> l = []
>>>> for i in range(2):
> for j in range(2):
> l[i][j] = 'x'
>
>
>
> Traceback (most recent call last):
> File "<pyshell#7>", line 3, in -toplevel-
> l[i][j] = 'x'
> IndexError: list index out of range
>
> So you still have to dimension the list before you can use it , eg like
>>l = []
>>for i in range(2):
>>    l.append([])
>>    for j in range(2):
>>        l[i].append(j)
>
> then you do not get the indexerror, but I feel a class is what you need
> here to make life easier and the program more readable

I'd probably numeric python (which I beleive is depricated, but
it's what I have installed) or numarray.  I think either would
make it a lot easier.  Using lists of lists when what you want
is a matrix can be made to work, but there are a lot of
non-obvious traps to fall into.

-- 
Grant Edwards                   grante             Yow!  I always wanted a
                                  at               NOSE JOB!!
                               visi.com            



More information about the Python-list mailing list