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


Groups > comp.lang.python > #32751

Re: Multi-dimensional list initialization

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <andrew3@r3dsolutions.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.032
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; 'operator': 0.03; 'initialize': 0.05; 'nested': 0.07; 'received:64.202.165': 0.07; 'python': 0.09; 'agree,': 0.09; 'be:': 0.09; 'replication': 0.09; 'thread,': 0.09; '[none]': 0.16; 'libs': 0.16; 'wrote:': 0.17; 'instance': 0.17; '3.2': 0.22; 'precise': 0.22; 'produces': 0.22; 'subject:skip:i 10': 0.22; 'defined': 0.22; 'references': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'guess': 0.27; 'question': 0.27; 'subject:list': 0.28; 'related': 0.30; 'asked': 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; 'list': 0.35; 'so,': 0.35; 'pm,': 0.35; 'there': 0.35; 'list.': 0.35; 'but': 0.36; 'anything': 0.36; "i'll": 0.36; 'beyond': 0.37; 'being': 0.37; 'why': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'copying': 0.38; 'several': 0.39; 'to:addr:python.org': 0.39; 'subject:-': 0.40; 'think': 0.40; 'easy': 0.60; 'real': 0.61; 'first': 0.61; 'more': 0.63; 'behavior': 0.64; 'here': 0.65; 'watching': 0.65; 'header:Reply-To:1': 0.68; 'answer.': 0.71; 'reply-to:no real name:2**0': 0.72; '"oh,': 0.84; 'nice,': 0.84; 'watched': 0.84; '"that\'s': 0.91; '(running': 0.91; 'was:': 0.91
Date Sun, 04 Nov 2012 22:44:39 -0800
From Andrew Robinson <andrew3@r3dsolutions.com>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:8.0) Gecko/20111126 Thunderbird/8.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Multi-dimensional list initialization
References <A399B195-7ED3-42D6-A54F-299FE66EBFAE@gmail.com>
In-Reply-To <A399B195-7ED3-42D6-A54F-299FE66EBFAE@gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To andrew3@r3dsolutions.com
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3270.1352097996.27098.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352097996 news.xs4all.nl 6974 [2001:888:2000:d::a6]:33172
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32751

Show key headers only | View raw


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?
:/

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web