Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'operator': 0.03; 'say,': 0.05; 'element': 0.07; 'great.': 0.07; 'modifying': 0.07; 'question.': 0.14; 'innermost': 0.16; 'kern': 0.16; 'luis': 0.16; 'numpy': 0.16; 'operation,': 0.16; 'underlying': 0.16; 'url:faq': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'replacing': 0.19; 'header :User-Agent:1': 0.23; 'forming': 0.24; 'lets': 0.24; "shouldn't": 0.24; 'header:In-Reply-To:1': 0.27; 'array': 0.29; 'robert': 0.30; 'sets': 0.30; 'received:132': 0.31; 'anyone': 0.31; 'lists': 0.32; 'url:python': 0.33; 'position.': 0.33; 'problem': 0.35; 'problem.': 0.35; 'but': 0.35; 'add': 0.35; 'explains': 0.36; 'in:': 0.36; 'set.': 0.36; 'doing': 0.36; 'url:org': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'explain': 0.39; 'obtain': 0.39; 'to:addr:python.org': 0.39; 'expression': 0.60; 'header :Return-path:1': 0.60; 'hope': 0.61; "you're": 0.61; 'here': 0.66; 'results': 0.69; 'union': 0.69; 'received:93': 0.72; 'as:': 0.81; 'subject:Sets': 0.84; 'want:': 0.84 Delivery-date: Sun, 25 May 2014 00:26:02 +0200 Date: Sun, 25 May 2014 00:25:55 +0200 From: Wolfgang Maier User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Numpy Array of Sets References: <38836877-cd87-44ce-b9df-1eda702e7164@googlegroups.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1400970363 news.xs4all.nl 2859 [2001:888:2000:d::a6]:47458 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:71976 On 25.05.2014 00:14, Robert Kern wrote: > On 2014-05-24 23:05, Luis José Novoa wrote: >> Hi All, >> >> Hope you're doing great. One quick question. I am defining an array of >> sets using numpy as: >> >> a=array([set([])]*3) >> Has nothing to do with numpy, but the problem is exclusively with your innermost expression [set([])]*3. >> Now, if I want to add an element to the set in, lets say, a[0], and I >> use the .add(4) operation, which results in: >> with .add you are modifying the *existing* set. >> array([set([4]), set([4]), set([4])], dtype=object) >> >> which I do not want. If I use the union operator >> >> a[0] = a[0] | set([4]) >> here you are forming a *new* set and put it in a[0] replacing the old set at this position. >> then I obtain what I want: >> >> array([set([4]), set([]), set([])], dtype=object) >> >> Can anyone explain whay this happens? > > Same reason why you shouldn't make a list of lists like so: [[]]*3 > > https://docs.python.org/2/faq/programming.html#how-do-i-create-a-multidimensional-list > The above link explains the underlying problem. Best, Wolfgang