[Numpy-discussion] setmember1d: docstring vs. code
Charles R Harris
charlesr.harris at gmail.com
Sat Mar 10 17:16:35 EST 2007
On 3/10/07, Robert Kern <robert.kern at gmail.com> wrote:
>
> Jouni K. Seppänen wrote:
>
> > 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.
>
> This is the current docstring:
>
> In [2]: setmember1d?
> Type: function
> Base Class: <type 'function'>
> Namespace: Interactive
> File:
> /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/numpy-
> 1.0.2.dev3569-py2.5-macosx-10.3-fat.egg/numpy/lib/arraysetops.py
> Definition: setmember1d(ar1, ar2)
> Docstring:
> Return a boolean array of shape of ar1 containing True where the
> elements
> of ar1 are in ar2 and False otherwise.
>
> Use unique1d() to generate arrays with only unique elements to use as
> inputs
> to this function.
>
> :Parameters:
> - `ar1` : array
> - `ar2` : array
>
> :Returns:
> - `mask` : bool array
> The values ar1[mask] are in ar2.
>
> :See also:
> numpy.lib.arraysetops has a number of other functions for performing
> set
> operations on arrays.
>
> > I would prefer the latter choice (but perhaps the
> > current code has some advantages).
>
> Well, it has the advantage of existing. If you have an implementation that
> is
> just as efficient, but works for general arrays, I'd love to see it.
Well, the version I posted awhile back works for general arrays. How
efficient it is compared to the current version I haven't investigated. For
reference, here it is again.
def ismember(ar1, ar2) :
a = sort(ar2)
il = a.searchsorted(ar1, side='left')
ir = a.searchsorted(ar1, side='right')
return ir != il
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070310/8930aacc/attachment.html>
More information about the NumPy-Discussion
mailing list