Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python': 0.09; 'referencing': 0.09; 'rounding': 0.09; 'sub': 0.09; 'def': 0.10; ';-)': 0.11; 'thread': 0.11; 'do,': 0.15; '*any*': 0.16; 'assignments': 0.16; 'copied,': 0.16; 'copied.': 0.16; 'nest': 0.16; 'quadratic': 0.16; 'shallow': 0.16; 'wrote:': 0.17; "shouldn't": 0.17; '>>>': 0.18; 'memory': 0.18; 'changes': 0.20; 'import': 0.21; 'meant': 0.21; 'subject:skip:i 10': 0.22; 'needed.': 0.23; 'thus': 0.24; 'header:In-Reply-To:1': 0.25; 'skip:[ 10': 0.26; 'wrote': 0.26; 'am,': 0.27; 'andrew': 0.27; 'message-id:@mail.gmail.com': 0.27; 'subject:list': 0.28; 'all.': 0.28; 'time;': 0.29; 'objects': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'received:209.85.215.46': 0.30; 'lists': 0.31; 'not.': 0.32; "aren't": 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'lists.': 0.35; 'nov': 0.35; 'received:209.85': 0.35; 'list.': 0.35; 'really': 0.36; 'but': 0.36; 'level': 0.37; 'rather': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'sure': 0.38; 'shows': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'where': 0.40; 'subject:-': 0.40; 'header:Received:5': 0.40; 'behavior': 0.64; 'become': 0.65; 'middle': 0.66; 'down:': 0.84; 'number):': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=merJIx4PKpzurPBoE+w4eJQbQSeIzxHSr+jEcslKTJI=; b=bVPVnAGudle1SXXPBA9U0z5qxxCHskMBFAijU9ZVgo8v5QdDB6CLhyS86ZI75Harpg nLc1HWJTwOcCbUCkDDh7yN/J0szMQVtY5moYjcDobA/cd7dfAGNlsUJAPDgodQ9kOWSO zNZpkeq1F4dt2x4zP/0y0z0y2o7/Xd/rDApQCZOwIz/6suG40v5ThM8yQjUChOfAAZ1f M19fqWLggHD0rRCzjhYCmvvTDXrG6ztQ0mQpkfFstSnVQTiUA15M0soci/pX6XAWOvKl aDO3cBysjQiPKTaHrRSAlaUePEVqVhMocuJNvZJKGW+TGoNIJ7/gIMJCbKcj7AF/xx45 g9/A== MIME-Version: 1.0 In-Reply-To: <5098C873.2000200@r3dsolutions.com> References: <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <5098A55C.3090201@r3dsolutions.com> <5098C873.2000200@r3dsolutions.com> From: Ian Kelly Date: Tue, 6 Nov 2012 02:19:12 -0700 Subject: Re: Multi-dimensional list initialization To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 66 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352193584 news.xs4all.nl 6964 [2001:888:2000:d::a6]:55654 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32812 On Tue, Nov 6, 2012 at 1:21 AM, Andrew Robinson wrote: > If you nest it another time; > [[[None]]]*4, the same would happen; all lists would be independent -- but > the objects which aren't lists would be refrenced-- not copied. > > a=[[["alpha","beta"]]]*4 would yield: > a=[[['alpha', 'beta']], [['alpha', 'beta']], [['alpha', 'beta']], [['alpha', > 'beta']]] > and a[0][0]=1 would give [[1],[['alpha', 'beta']], [['alpha', 'beta']], > [['alpha', 'beta']]]] > rather than a=[[1], [1], [1], [1]] > > Or at another level down: a[0][0][0]=1 would give: a=[[[1, 'beta']], > [['alpha', 'beta']], [['alpha', 'beta']], [['alpha', 'beta']] ] > rather than a=[[[1, 'beta']], [[1, 'beta']], [[1, 'beta']], [[1, 'beta']]] You wrote "shallow copy". When the outer-level list is multiplied, the mid-level lists would be copied. Because the copies are shallow, although the mid-level lists are copied, their contents are not. Thus the inner-level lists would still be all referencing the same list. To demonstrate: >>> from copy import copy >>> class ShallowCopyList(list): ... def __mul__(self, number): ... new_list = ShallowCopyList() ... for _ in range(number): ... new_list.extend(map(copy, self)) ... return new_list ... >>> a = ShallowCopyList([[["alpha", "beta"]]]) >>> a [[['alpha', 'beta']]] >>> b = a * 4 >>> b [[['alpha', 'beta']], [['alpha', 'beta']], [['alpha', 'beta']], [['alpha', 'beta']]] >>> b[0][0][0] = 1 >>> b [[[1, 'beta']], [[1, 'beta']], [[1, 'beta']], [[1, 'beta']]] >>> b[0][0] = 1 >>> b [[1], [[1, 'beta']], [[1, 'beta']], [[1, 'beta']]] This shows that assignments at the middle level are independent with a shallow copy on multiplication, but assignments at the inner level are not. In order to achieve the behavior you describe, a deep copy would be needed. > That really is what people *generally* want. > If the entire list is meant to be read only -- the change would affect > *nothing* at all. The time and memory cost of the multiplication operation would become quadratic instead of linear. > See if you can find *any* python program where people desired the > multiplication to have the die effect that changing an object in one of the > sub lists -- changes all the objects in the other sub lists. > > I'm sure you're not going to find it -- and even if you do, it's going to be > 1 program in 1000's. Per the last thread where we discussed extremely rare scenarios, shouldn't you be rounding "1 in 1000s" up to 20%? ;-)