LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-14-2008, 09:10 AM   #1
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
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...

Last edited by J_Szucs; 06-14-2008 at 09:25 AM.
 
Old 06-14-2008, 05:02 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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.

Last edited by ntubski; 06-14-2008 at 05:18 PM.
 
Old 06-15-2008, 03:48 AM   #3
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Original Poster
Rep: Reputation: 58
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)...

Last edited by J_Szucs; 06-15-2008 at 04:03 AM.
 
Old 06-15-2008, 04:33 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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?)".
 
  


Reply

Tags
python, sort



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python list.sort() buggy? notnor Programming 3 05-18-2008 12:29 AM
selection sort compiles but does not sort the array as desired ganesha Programming 2 04-20-2008 07:44 AM
Python waiting function... sort of? billyseth Programming 1 04-25-2007 05:36 PM
php array sort inconsistent results rblampain Programming 2 04-03-2006 12:34 AM
array logical operations in python? zero79 Programming 2 05-13-2005 03:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration