[Numpy-discussion] setmember1d: docstring vs. code

Jouni K. Seppänen jks@iki...
Sat Mar 10 07:15:22 CST 2007


Hi,

According to an earlier posting on this list, the elements of the
first argument to setmember1d are assumed to be unique:

http://thread.gmane.org/gmane.comp.python.numeric.general/13251/focus=13307

The docstring for setmember1d does not state this:

| Definition:     numpy.setmember1d(ar1, ar2)
| Docstring:
|     Return an array of shape of ar1 containing 1 where the elements of
|     ar1 are in ar2 and 0 otherwise.
|     
|     See also: ediff1d, unique1d, intersect1d, intersect1d_nu, setxor1d,
|     union1d, setdiff1d

The numpy book has a slightly different description, which claims that
setmember1d(tocheck, set) is "[e]quivalent to array([x in set for x in
tocheck])." This is not true when tocheck has repeated elements:

In [29]:tocheck = [1,3,2,3]

In [30]:set = [2]

In [31]:numpy.setmember1d(tocheck, set)
Out[31]:array([False, True, True, False], dtype=bool)

In [32]:array([x in set for x in tocheck])
Out[32]:array([False, False, True, False], dtype=bool)

I think that either the docstring (and the book) should be corrected
to mention the assumption, or the code should be made to work in the
arbitrary case. I would prefer the latter choice (but perhaps the
current code has some advantages).

-- 
Jouni K. Seppänen
http://www.iki.fi/jks



More information about the Numpy-discussion mailing list