473,327 Members | 2,118 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

my array subset method could be improved?

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

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
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
anybody know any good midi tutorials for python?
i am trying to make a realtime midi manipulation application.
Oct 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Michael Drumheller | last post by:
(If you're not interested in NumArray, please skip this message.) I am new to NumArray and I wonder if someone can help me with array-indexing. Here's the basic situation: Given a rank-2 array...
2
by: Dave Bazell | last post by:
I have a multidimensional array, say 20 x 6, and I want to take a subset of the rows. I have an index array @indx=(1,3,12,17) for example, and what I want to say is @new_array = $data I know...
4
by: Jim O'D | last post by:
Hi all I have an array a=array(). I want to extract an array with all the elements of a that are less than 0. Method 1. new = array() Method 2.
6
by: Denis C | last post by:
Hi I have a byte array of length 2404. I want to access subsets of the array and pass the subset to the constructor of a class. i.e. Dim bData(2403) As Byte Dim aThing As SomeClass = new...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
7
by: Jacob JKW | last post by:
I need to iterate over combinations of n array elements taken r at a time. Because the value of r may vary quite a bit between program invocations, I'd like to avoid simply hardcoding r loops. I...
5
by: M. Fisher | last post by:
Pardon my ignorance here... I have created arrays such as: var SDET_Lab130= new Array(); SDET_Lab130 = new Array(); SDET_Lab130 = ; SDET_Lab130 = ; SDET_Lab130 = ; SDET_Lab130 = ;...
57
by: buuuuuum | last post by:
why array can't be assigned, like structs?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.