By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

array subset could be improved? -repost ;)

P: n/a
Hi all

I have an array a=array([2,3,-1]).

I want to extract an array with all the elements of a that are less than 0.

Method 1.
new = array([i for i in a if i < 0])

Method 2.
new = a[nonzero(a<0)]

I'm using Numeric arrays but can't seem to find a function that does this.

Am I missing a more obvious way to do it quickly?

Thanks

Jim
Oct 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
jon

Jim O'D wrote:
Hi all

I have an array a=array([2,3,-1]).

I want to extract an array with all the elements of a that are less than 0.

Method 1.
new = array([i for i in a if i < 0])

Method 2.
new = a[nonzero(a<0)]

I'm using Numeric arrays but can't seem to find a function that does this.

Am I missing a more obvious way to do it quickly?

Thanks

Jim


Oct 14 '05 #2

P: n/a
jon

Jim O'D wrote:
Hi all

I have an array a=array([2,3,-1]).

I want to extract an array with all the elements of a that are less than 0.

Method 1.
new = array([i for i in a if i < 0])

Method 2.
new = a[nonzero(a<0)]

I'm using Numeric arrays but can't seem to find a function that does this.

Am I missing a more obvious way to do it quickly?

Thanks

Jim


a2=Numeric.compress(a<0,a)

Oct 14 '05 #3

P: n/a
Jim O'D wrote:
Hi all

I have an array a=array([2,3,-1]).

I want to extract an array with all the elements of a that are less than 0.


Numeric is currently changing into the new scipy core. If you are willing to
play with beta code, get it here:

http://numeric.scipy.org

if not, wait a little for an official release.

With the new numeric, you'll be able to do:

negatives = a[a<0]

Cheers,

f

Oct 14 '05 #4

P: n/a
With the new numeric, you'll be able to do:

negatives = a[a<0]

Cheers,

f


Ooh, that's nice.

Jim
Oct 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.