Numpy Array of Sets

Robert Kern robert.kern at gmail.com
Sat May 24 18:14:32 EDT 2014


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)
>
> 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:
>
> 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])
>
> 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

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list