Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #71976
| Date | 2014-05-25 00:25 +0200 |
|---|---|
| From | Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> |
| Subject | Re: Numpy Array of Sets |
| References | <38836877-cd87-44ce-b9df-1eda702e7164@googlegroups.com> <llr5k8$3n6$1@ger.gmane.org> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.10273.1400970363.18130.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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