Sorting coordinates array
Peter Otten
__peter__ at web.de
Fri Dec 19 06:01:53 EST 2003
More information about the Python-list mailing list
Fri Dec 19 06:01:53 EST 2003
- Previous message (by thread): udp broadcast example, please
- Next message (by thread): Sorting coordinates array
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Previous message (by thread): udp broadcast example, please
- Next message (by thread): Sorting coordinates array
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list