Regarding sort()

Neil Crighton neilcrighton at gmail.com
Mon May 25 05:50:39 EDT 2009


Dhananjay <dhananjay.c.joshi <at> gmail.com> writes:

> I want to sort the data on the basis of 3rd column first and latter want to 
> sort the sorted data (in first step) on the basis of 6th column.I tried 
> sort() function but could not get the way how to use it.I am new to 
> programming, please tell me how can I sort.Thanking you in advance 


If you want to use numpy, you can do this (in version 1.2 or above):

Assuming the columns of data you posted are in a text file called temp.txt,

>>> import numpy as np

Read the file to a numpy array and convert to int, float or string as
appropriate. Each column is given a name - 'f0' is the first column, 'f1' is the
second column, and so on.

>>> data = np.genfromtxt('temp.txt', dtype=None)

Sort rows by the 3rd column, and if the 3rd column values are the same, sort by
the 6th column:

>>> data.sort(order=['f2', 'f5'])



Neil






More information about the Python-list mailing list