LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Sort an array in python (http://www.linuxquestions.org/questions/programming-9/sort-an-array-in-python-649252/)

 J_Szucs 06-14-2008 10:10 AM

Sort an array in python

I am a green noob to python, and it looks like I cannot find a solution myself :(

I have a two-dimensional array like this (sorry but for some reason I cannot not produce square brackets in firefox to describe the arrays correctly - but that's an other issue):
dist(0)(0) = 25
dist(0)(1) = 1
dist(1)(0) = 12
dist(1)(1) = 2
dist(2)(0) = 11
dist(2)(1) = 3

Basically, column0 of the array contains values, column1 contains indexes.

What I want to do is to sort the above array on the values (column0), and then keep only the first (say) two rows, like this:

dist(0)(0) = 11
dist(0)(1) = 3
dist(1)(0) = 12
dist(1)(1) = 2

I just cannot find any simple methods to sort an array like this, and the examples I found in the python docs (e.g. with itemgetter) do not seem to work for me...

 ntubski 06-14-2008 06:02 PM

Code:

```>>> dist = [[25, 1],                                                                                                                                                                                                          [12, 2],                                                                                                                                                                                                              [11, 3]] ... ... >>> >>> dist.sort(key=lambda a: a[0]) # key function only looks at first element in row ie column 0 >>> dist [[11, 3], [12, 2], [25, 1]] >>> dist = dist[0:2]  # take only the first 2 rows >>> dist [[11, 3], [12, 2]]```
EDIT: I looked up that itemgetter thing, you can replace lamba a: a[0] with operator.itemgetter(0) (after importing operator), if you care about efficiency.

 J_Szucs 06-15-2008 04:48 AM

Traceback (most recent call last):
File "im.py", line 37, in ?
dist.sort(key=lambda a: a[0])
AttributeError: sort

With the operator.itemgetter(0) thingy, strings like "array(0)(0) array(0)(1) ..." seem to replace the original elements of the array, and that's not the right behaviour, either :(

Here are the modules the script imports:
import Image
from Numeric import *
import operator

Should it import something more for the lambda thingy to work?

In the meantime I inserted the good old sorting algorithm into the python script, but that works badly slowly with the array (in real life the array has 80 rows and 2 colums, and there are 180 such arrays to sort)...

 ntubski 06-15-2008 05:33 PM

Can you post your code, it's difficult to figure out what your problem is otherwise.

What version do you have (output from python --version)? from http://docs.python.org/lib/typesseq-...pesseq-mutable
Quote:
 Changed in version 2.4: Support for key and reverse was added.
If you have an earlier version,
Code:

`dist.sort(cmp=lambda x y: cmp(x[0], y[0]))`
should still work. A bit slower, but probably still faster than "the good old sorting algorithm(bubblesort?)".

 All times are GMT -5. The time now is 11:19 PM.