Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4a.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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'attribute': 0.07; 'modify': 0.07; 'modifying': 0.07; 'immutable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'set()': 0.16; 'elements': 0.16; 'modification': 0.16; 'prevent': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:User- Agent:1': 0.23; '(for': 0.26; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'reply.': 0.31; '"",': 0.31; 'assert': 0.31; 'fine,': 0.31; 'values.': 0.31; 'file': 0.32; '(most': 0.33; 'but': 0.35; 'there': 0.35; 'should': 0.36; 'example,': 0.37; 'so,': 0.37; 'thank': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'ensure': 0.60; 'skip:n 30': 0.60; "you're": 0.61; 'obvious': 0.74; 'or:': 0.84; 'subject:Sets': 0.84; 'canonical': 0.91; 'problems?': 0.91; 'trouble.': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Numpy Array of Sets Date: Sun, 25 May 2014 17:12:25 +0200 Organization: None References: <38836877-cd87-44ce-b9df-1eda702e7164@googlegroups.com> <9929f123-705c-4656-a400-171e48935244@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd9f71.dip0.t-ipconnect.de User-Agent: KNode/4.11.5 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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1401030762 news.xs4all.nl 2902 [2001:888:2000:d::a6]:45149 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:72012 LJ wrote: > Thank you for the reply. > > So, as long as I access and modify the elements of, for example, > > A=array([[set([])]*4]*3) > > > as (for example): > > a[0][1] = a[0][1] | set([1,2]) > > or: > > a[0][1]=set([1,2]) > > then I should have no problems? As long as you set (i. e. replace) elements you're fine, but modifying means trouble. You can prevent accidental modification by using immutable values -- in your case frozenset: >>> b = numpy.array([[frozenset()]*4]*3) >>> b[0,0].update("123") Traceback (most recent call last): File "", line 1, in AttributeError: 'frozenset' object has no attribute 'update' Or you take the obvious approach and ensure that there are no shared values. I don't know if there's a canonical form to do this in numpy, but >>> a = numpy.array([[set()]*3]*4) >>> a |= set() works: >>> assert len(set(map(id, a.flat))) == 3*4