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

my array subset method could be improved?

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
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


new = Numeric.compress(Numeric.less(a,0),a)
Oct 14 '05 #2

P: n/a

new = Numeric.compress(Numeric.less(a,0),a)


Ah, thank you!

Sorry about subject mangle, I gave a silly example first time round.

Jim
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.

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

You could use filter (which is planned to be removed in Py3000). E.g.
"array(filter(lambda x: x < 0, (-1, 2, -4, 5, 55, 9, -73, 5)))"

I tend to like "array(i for i in (-1, 2, -4, 5, 55, 9, -73, 5) if i <
0)" better though. (And its even shorter).

HTH,
Peter

Oct 14 '05 #4

P: n/a
anybody know any good midi tutorials for python?
i am trying to make a realtime midi manipulation application.
Oct 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.