indexes in an array where value is greater than 1?

Hi All,
I have an array:
arrrgh = numpy.zeros(100000000)
A sparse collection of elements will have values greater than zero:
arrrgh[9999] = 2 arrrgh[3453453] =42
The *wrong* way to do this is:
for i in xrange(len(arrrgh)): if arrrgh[i] > 1: print i
What's the right way?
Chris

On Fri, May 25, 2012 at 11:17 AM, Chris Withers chris@simplistix.co.ukwrote:
Hi All,
I have an array:
arrrgh = numpy.zeros(100000000)
A sparse collection of elements will have values greater than zero:
arrrgh[9999] = 2 arrrgh[3453453] =42
The *wrong* way to do this is:
for i in xrange(len(arrrgh)): if arrrgh[i] > 1: print i
What's the right way?
Chris
np.nonzero(arrrgh > 1)
Note, it returns a list of lists, one for each dimension of the input array.
Cheers! Ben Root

On 25/05/2012 16:21, Benjamin Root wrote:
np.nonzero(arrrgh > 1)
Did you mean np.where(arrrgh > 1)? I didn't know you could use np.nonzero in the way your describe?
Chris

for me, np.nonzero() and np.where() both work. It seems they have same function.
chao
2012/5/27 Chris Withers chris@simplistix.co.uk
On 25/05/2012 16:21, Benjamin Root wrote:
np.nonzero(arrrgh > 1)
Did you mean np.where(arrrgh > 1)?for I didn't know you could use np.nonzero in the way your describe?
Chris
-- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

On Sunday, May 27, 2012, Chao YUE wrote:
for me, np.nonzero() and np.where() both work. It seems they have same function.
chao
They are not identical. Nonzeros is for indices. The where function is really meant for a different purpose, but special-cases for this call signature.
Ben Root
participants (3)
-
Benjamin Root
-
Chao YUE
-
Chris Withers