ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> numpy-discussion
numpy-discussion
Re: [Numpy-discussion] Indices returned by where()
by Charles R Harris other posts by this author
Dec 6 2006 10:18PM messages near this date
[Numpy-discussion] Indices returned by where() | [Numpy-discussion] Resizing without allocating additional memory
On 12/6/06, Christian Marquardt <christian@[...].sc>  wrote:
> 
>  Dear list,
> 
>  apologies if the answer to my question is obvious...
> 
>  Is the following intentional?
> 
>    $>python
> 
>    Python 2.4 (#1, Mar 22 2005, 21:42:42)
>    [GCC 3.3.5 20050117 (prerelease) (SUSE Linux)] on linux2
>    Type "help", "copyright", "credits" or "license" for more information.
> 
>    >>> import numpy as np
>    >>> print np.__version__
>    1.0
> 
>    >>> x = np.array([1., 2., 3., 4., 5.])
> 
>    >>> idx = np.where(x > 6.)
>    >>> print len(idx)
>    1
> 
>  The reason is of course that where() returns a tuple of index arrays
>  instead of simply an index array:
> 
>    >>> print idx
>    (array([], dtype=int32),)
> 
>  Does that mean that one always has to explicitely request the first
>  element of the returned tuple in order to check how many matches were
>  found, even for 1d arrays? What's the reason for designing it that way?


Fancy indexing.

In [1]: a = arange(10).reshape(2,5)

In [2]: i = where(a> 3)

In [3]: i
Out[3]: (array([0, 1, 1, 1, 1, 1]), array([4, 0, 1, 2, 3, 4]))

In [4]: a[i] = 0

In [5]: a
Out[5]:
array([[0, 1, 2, 3, 0],
       [0, 0, 0, 0, 0]])


If you just want a count, try

In [6]: a = arange(10).reshape(2,5)

In [7]: sum(a> 3)
Out[7]: 6

Chuck
Thread:
Christian Marquardt
Charles R Harris

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved