[Numpy-discussion] argwhere does not accept py list

Scott Sinclair scott.sinclair.za at gmail.com
Fri Jul 3 06:44:55 EDT 2009


>2009/7/3 Sebastian Haase <seb.haase at gmail.com>:
> Hi,
> should this not be accepted:
>>>> N.argwhere([4,0,2,1,3])
> ?
> instead I get
>
> Traceback (most recent call last):
>  File "<input>", line 1, in <module>
>  File "./numpy/core/numeric.py", line 510, in argwhere
> AttributeError: 'list' object has no attribute 'nonzero'
>>>> N.argwhere(N.array([4,0,2,1,3]))
> [[0]
>  [2]
>  [3]
>  [4]]
>>>> N.__version__
> '1.3.0'
>>>>

A fix could be a simple as applying the following diff (or similar + tests)

Index: numpy/core/numeric.py
===================================================================
--- numpy/core/numeric.py	(revision 7095)
+++ numpy/core/numeric.py	(working copy)
@@ -535,7 +535,7 @@
            [1, 2]])

     """
-    return asarray(a.nonzero()).T
+    return transpose(asarray(a).nonzero())

>>> import numpy as np
>>> a = [4,0,2,1,3]
>>> np.argwhere(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/scott/.virtualenvs/numpy-dev/lib/python2.6/site-packages/numpy/core/numeric.py",
line 538, in argwhere
    return asarray(a.nonzero()).T
AttributeError: 'list' object has no attribute 'nonzero'
>>> np.argwhere(np.asarray(a))
array([[0],
       [2],
       [3],
       [4]])
>>> np.transpose(np.asarray(a).nonzero())
array([[0],
       [2],
       [3],
       [4]])
>>>

Cheers,
Scott



More information about the NumPy-Discussion mailing list