* y for y in range(3) ] * x for x in range(2) ]
Author Message
* y for y in range(3) ] * x for x in range(2) ]

Hi,

I'm trying to allocate nested lists up-front using the subject line,
[ [ [], [], [] ], [ [], [], [] ] ]
I end up with  :
[ [], [ [], [], [] ] ]

I guess I screwed up something, but I'm failing to understand what...

Any idea ?

thanks
Marcel

Mon, 18 Jul 2005 12:34:05 GMT
* y for y in range(3) ] * x for x in range(2) ]

Quote:

> Hi,

> I'm trying to allocate nested lists up-front using the subject line,
> [ [ [], [], [] ], [ [], [], [] ] ]
> I end up with  :
> [ [], [ [], [], [] ] ]

> I guess I screwed up something, but I'm failing to understand what...

> Any idea ?

Try:
[[[] for y in range(3)] for x in range(2)]

You don't want to multiply the lists, which copies the contents -- in fact,
multiplying an empty list by anything will always give you an empty list.

Why are you pre-allocating the lists, anyway?  The speed difference should
be negligible to just doing someList.append(..) as appropriate.

-Andrew.

Mon, 18 Jul 2005 13:09:42 GMT
* y for y in range(3) ] * x for x in range(2) ]

Marcel> Hi, I'm trying to allocate nested lists up-front using the
Marcel> subject line, instead of generating : [ [ [], [], [] ], [ [],
Marcel> [], [] ] ] I end up with : [ [], [ [], [], [] ] ]

Marcel> I guess I screwed up something, but I'm failing to understand
Marcel> what...

Marcel> Any idea ?

Hint: range(3) means [0,1,2], so [[]*y for y in [0,1,2]] has first element
[], second [[]], third [[], []].

Regards,
Isaac.

Mon, 18 Jul 2005 13:00:19 GMT
* y for y in range(3) ] * x for x in range(2) ]

Quote:

> I'm trying to allocate nested lists up-front using the subject line,
> [ [ [], [], [] ], [ [], [], [] ] ]
> I end up with  :
> [ [], [ [], [], [] ] ]

What's wrong with [[[]]*3]*2  ?

--
David Eppstein       UC Irvine Dept. of Information & Computer Science

Mon, 18 Jul 2005 14:01:51 GMT
* y for y in range(3) ] * x for x in range(2) ]
This is whats wrong.

Quote:
>>> L=[[[]]*3]*2
>>> L

[[[], [], []], [[], [], []]]
Quote:
>>> L[0][0]=1
>>> L

[[1, [], []], [1, [], []]]

Chirayu.

On Wed, 29 Jan 2003 22:01:51 -0800, David Eppstein

Quote:

>> I'm trying to allocate nested lists up-front using the subject line,
>> [ [ [], [], [] ], [ [], [], [] ] ]
>> I end up with  :
>> [ [], [ [], [], [] ] ]

>What's wrong with [[[]]*3]*2  ?

Mon, 18 Jul 2005 14:37:04 GMT
* y for y in range(3) ] * x for x in range(2) ]

Quote:
> This is whats wrong.

> >>> L=[[[]]*3]*2
> >>> L
> [[[], [], []], [[], [], []]]
> >>> L[0][0]=1
> >>> L
> [[1, [], []], [1, [], []]]

Ok, you want a 2x3 table of 6 distinct empty lists...

[[[] for x in range(3)] for y in range(2)]

--
David Eppstein       UC Irvine Dept. of Information & Computer Science

Mon, 18 Jul 2005 14:51:18 GMT
* y for y in range(3) ] * x for x in range(2) ]

Quote:
> What's wrong with [[[]]*3]*2  ?

For the inner list, [[]]*3 is the same as [[] for y in range(3)].  For
the outer list, a second 'multiplication', as you must have just
forgotten, is a common newbie mistake leading to multiple references
to the *same* inner list and surprises like the following:

Quote:
>>> a=[[[]]*3]*2
>>> a[0][0]=2
>>> a

[[2, [], []], [2, [], []]]

To have independent inner lists, each must be created separately.

Quote:
>>> a = [[[]]*3,[[]]*3]
>>> a[0][0]=[2]
>>> a

[[[2], [], []], [[], [], []]]

More generally:
a = [[[]]*m for x in range(n)] # n rows of m empty lists.

Terry J. Reedy

Mon, 18 Jul 2005 15:00:46 GMT
* y for y in range(3) ] * x for x in range(2) ]

Quote:

>This is whats wrong.

>>>> L=[[[]]*3]*2
>>>> L
>[[[], [], []], [[], [], []]]
>>>> L[0][0]=1
>>>> L
>[[1, [], []], [1, [], []]]

Exactly.  To put it another way:

L = [ [[]]*3 ] * 2

is equivalent to

a = []
b = [a, a, a]
L = [b, b]

so that L[0] and L[1] refer to the same object -- and each
of the three items in that object refer to another unique
object.  Whatever you do to one, you do to the other(s), in
most cases.

One thing that will break the symmetry is to assign
something different to either L[0] or L[1].  This is
effectively what all the other solutions do:  make sure that
the elements of L refer to different list objects from the
outset.

L[1] = [17]

or

L[1] = [[],[],[]]

for instance, would remove part of the problem by making
L[1] refer to a brand-new list object, although (assuming
`b`, used above, hasn't been re-bound in the meantime)

c = b
L[1] = c

would leave us right where we were before.

It seems my post is just a long-winded way of avoiding
talking about references and PyObjects and the fact that the
effect of `=` is just to associate a name (in the form of a
character string) to a reference to an object, using some
particular dictionary.  Maybe it'll shed some light anyway.

Regards.        Mel.

Mon, 18 Jul 2005 22:46:41 GMT

 Page 1 of 1 [ 8 post ]

Relevant Pages