ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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...
>>> 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.
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)...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.