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


Groups > comp.lang.python > #72012

Re: Numpy Array of Sets

From Peter Otten <__peter__@web.de>
Subject Re: Numpy Array of Sets
Date 2014-05-25 17:12 +0200
Organization None
References (1 earlier) <llr5k8$3n6$1@ger.gmane.org> <mailman.10273.1400970363.18130.python-list@python.org> <f55843c2-17f3-4551-a1c6-b608c09fd6d8@googlegroups.com> <mailman.10294.1401024422.18130.python-list@python.org> <9929f123-705c-4656-a400-171e48935244@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.10299.1401030762.18130.python-list@python.org> (permalink)

Show all headers | View raw


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 "<stdin>", line 1, in <module>
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

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


Thread

Numpy Array of Sets Luis José Novoa <luisjosenovoa@gmail.com> - 2014-05-24 15:05 -0700
  Re: Numpy Array of Sets Robert Kern <robert.kern@gmail.com> - 2014-05-24 23:14 +0100
  Re: Numpy Array of Sets Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2014-05-25 00:25 +0200
    Re: Numpy Array of Sets LJ <luisjosenovoa@gmail.com> - 2014-05-25 05:29 -0700
      Re: Numpy Array of Sets Peter Otten <__peter__@web.de> - 2014-05-25 15:26 +0200
        Re: Numpy Array of Sets LJ <luisjosenovoa@gmail.com> - 2014-05-25 07:14 -0700
          Re: Numpy Array of Sets Peter Otten <__peter__@web.de> - 2014-05-25 17:12 +0200
            Re: Numpy Array of Sets LJ <luisjosenovoa@gmail.com> - 2014-05-25 08:17 -0700

csiph-web