my array subset method could be improved?

J

Jim O'D

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
 
J

Juho Schultz

Jim said:
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)
 
P

Peter

Jim said:
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
 
M

malik

anybody know any good midi tutorials for python?
i am trying to make a realtime midi manipulation application.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top