Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32751 > unrolled thread

Re: Multi-dimensional list initialization

Started byAndrew Robinson <andrew3@r3dsolutions.com>
First post2012-11-04 22:44 -0800
Last post2012-11-04 22:44 -0800
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Multi-dimensional list initialization Andrew Robinson <andrew3@r3dsolutions.com> - 2012-11-04 22:44 -0800

#32751 — Re: Multi-dimensional list initialization

FromAndrew Robinson <andrew3@r3dsolutions.com>
Date2012-11-04 22:44 -0800
SubjectRe: Multi-dimensional list initialization
Message-ID<mailman.3270.1352097996.27098.python-list@python.org>
On 11/04/2012 10:27 PM, Demian Brecht wrote:
> So, here I was thinking "oh, this is a nice, easy way to initialize a 4D matrix" (running 2.7.3, non-core libs not allowed):
>
> m = [[None] * 4] * 4
>
> The way to get what I was after was:
>
> m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]]
FYI:  The behavior is the same in python 3.2
m=[[None]*4]*4
produces a nested list with all references being to the first instance 
of the inner list construction.

I agree, the result is very counter-intuitive; hmmm... but I think you 
meant:

m = [[None] * 4, [None] * 4, [None] * 4, [None] *4 ]
rather than:
m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]]

? :) ?

I asked a why question on another thread, and watched several dodges to 
the main question; I'll be watching to see if you get anything other 
than "That's the way it's defined in the API".  IMHO -- that's not a 
real answer.

My guess is that the original implementation never considered anything 
beyond a 1d list.
:)

A more precise related question might be: is there a way to force the 
replication operator to use copying rather than referencing?
:/

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web