multidimensional "arrays"

Remco Gerlich remco at gerlich.nl
Thu Dec 6 10:50:05 EST 2007


Hi,

DATA = [ [ 0 for i in range(ncolumns) ] for i in range(nrows) ]

Is one way.

DON'T do it like this:

row = [0] * ncolumns
data = [ row ] * nrows # WRONG!

Since after that, every row is the exact same object; if you set data[0][0]
= 1, the first element of _every_ row is 1.

But I guess you already found out about that :-)

That said, are you sure a list of lists is the best data structure for what
you're trying to do? I keep being surprised by Python code that uses other
data structures in clever ways; lists of lists seem to be pretty rare!

Remco Gerlich

On Dec 6, 2007 4:29 PM, Horacius ReX <horacius.rex at gmail.com> wrote:

> in python, when I want to use arrays, I follow this way;
>
> DATA = [0] * nint
>
> and when I want to use I do;
>
> ....
>
> DATA[i] = ....
>
> do you know how to do similar but in two dimensions ?
>
> thanks
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20071206/78a5c68b/attachment.html>


More information about the Python-list mailing list