473,378 Members | 1,134 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,378 software developers and data experts.

Sorting coordinates array

I'm a newbie to Python, so sorry for this maybe trivial question. I have a
numpy array with coordinates, which I want to sort, for example first on
z-coordinate, then x and lastly y-coordinate. So an array like:

[[0, 0, 0], [0, 2, 1], [1, 1, 0], [0, 1, 1]]

should look after sorting like

[[0, 0, 0], [1, 1, 0], [0, 1, 1], [0, 2, 1]]

I tried the sort command, but that command mangles my coordinate pairs. Also
converting the array to a list doesn't seem to improve the results. How
should I tackle this problem?

TIA, Maarten

--
================================================== =================
Maarten van Reeuwijk Heat and Fluid Sciences
Phd student dept. of Multiscale Physics
www.ws.tn.tudelft.nl Delft University of Technology
Jul 18 '05 #1
3 3910
Maarten van Reeuwijk <maarten@remove_this_ws.tn.tudelft.nl> writes:
I'm a newbie to Python, so sorry for this maybe trivial question. I have a
numpy array with coordinates, which I want to sort, for example first on
z-coordinate, then x and lastly y-coordinate. So an array like:


Implement a comparison function:
help([].sort)

Help on built-in function sort:

sort(...)
L.sort(cmpfunc=None) -- stable sort *IN PLACE*; cmpfunc(x, y) -> -1, 0, 1
Alternatively, you could take a look at DSU pattern (Decorate, Sort,
Undecorate):

http://aspn.activestate.com/ASPN/Coo...n/Recipe/52234

--
Ville Vainio http://www.students.tut.fi/~vainio24
Jul 18 '05 #2
Maarten van Reeuwijk wrote:
I'm a newbie to Python, so sorry for this maybe trivial question. I have a
numpy array with coordinates, which I want to sort, for example first on
z-coordinate, then x and lastly y-coordinate. So an array like:

[[0, 0, 0], [0, 2, 1], [1, 1, 0], [0, 1, 1]]

should look after sorting like

[[0, 0, 0], [1, 1, 0], [0, 1, 1], [0, 2, 1]]

I tried the sort command, but that command mangles my coordinate pairs.
Also converting the array to a list doesn't seem to improve the results.
How should I tackle this problem?

import Numeric
a = Numeric.array([[0, 0, 0], [0, 2, 1], [1, 1, 0], [0, 1, 1]])
lst = a.tolist()
lst.sort(lambda x, y: cmp(x[::-1], y[::-1]))
lst [[0, 0, 0], [1, 1, 0], [0, 1, 1], [0, 2, 1]]


You can pass an arbitrary comparison function to list.sort(), I just compare
reversed copies of the inner lists.
This is a highly unoptimized version, but at least seems to do the job.

Peter
Jul 18 '05 #3
Peter Otten wrote:
This is a highly unoptimized version, but at least seems to do the job.


Slightly better:
lst = a.tolist()
lst [[0, 0, 0], [0, 2, 1], [1, 1, 0], [0, 1, 1]] for item in lst: item.reverse() .... lst.sort()
for item in lst: item.reverse() .... lst [[0, 0, 0], [1, 1, 0], [0, 1, 1], [0, 2, 1]]


Peter
Jul 18 '05 #4

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

Similar topics

3
by: Berk Birand | last post by:
Hi, I am new to Perl (as a matter of fact, I started learning it a couple of days ago; I have some previous programming experience though). When looking for the ideal languages to use for a...
2
by: Oldchatterman | last post by:
Hello, in an application I measure a lot of 2d coordinates (x,y) of a pattern. This pattern consists of a set of points on grid with fixed pitches in x and y direction. These coordinates all...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
15
by: Tony Gahlinger | last post by:
I'm using my browser (Mozilla/5.0 Linux i686 Gecko/20031007 Firebird/0.7) to do some client-side image processing. I want to capture the sequence of coordinates a user clicks on in xxx.jpg in the...
3
by: acosgaya | last post by:
Hi, I would like some help as how to approach the following problem: I have a set of d-dimensional points (e.g (3,5,8,9,5) is a 5-dimensional point), and I need to sort the points on each of the...
1
by: PhilC | last post by:
''' ################################################ Task:- to apply a translation array to an array of 3D vertex coordinates to produce the resulting location of each vertices. Translation...
2
by: Jon Slaughter | last post by:
public class PointSorter : IComparer<PointF> { int IComparer<PointF>.Compare(PointF x, PointF y) { if (x.X y.X) return 1; if (x.X < y.X) return -1; return 0; }
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
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...
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.