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] equivalent to isempty command in matlab (newbie question)
by Filip Wasilewski other posts by this author
Dec 6 2006 11:04AM messages near this date
Re: [Numpy-discussion] equivalent to isempty command in matlab (newbie question) | Re: [Numpy-discussion] equivalent to isempty command in matlab (newbie question)
On 12/6/06, Robert Kern <robert.kern@[...].com>  wrote:
>  Filip Wasilewski wrote:
> 
>  > Just like for other Python objects:
>  >
>  > if ifs3:
>  >    print "not empty"
> 
>  No, that doesn't work. numpy arrays do not have a truth value. They raise an
>  error when you try to use them in such a context.

Right!

I could swear I have checked this before posting. Evidently I got
bitten by this:

> >> bool(numpy.array([]))
False
> >> bool(numpy.array([1]))
True
> >> bool(numpy.array([0]))
False
> >> bool(numpy.array([1,1]))

Traceback (most recent call last):
  File "<pyshell#8> ", line 1, in -toplevel-
    bool(numpy.array([1,1]))
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()

So depending on the situation one can use len or size:
> >> len(numpy.array([[],[]]))
2
> >> numpy.array([[],[]]).size
0


And how to understand following?

> >> print numpy.array([1,1]) == [], numpy.array([1,1]) != []
False True
> >> print `numpy.array([1]) == []`, `numpy.array([1]) != []`
array([], dtype=bool) array([], dtype=bool)
> >> print bool(numpy.array([1]) == []), bool(numpy.array([1]) != [])
False False


cheers,
fw
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion@[...].org
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Thread:
Giorgio Luciano
Filip Wasilewski
Robert Kern
Filip Wasilewski
Robert Kern

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