469,955 Members | 2,157 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,955 developers. It's quick & easy.

Re: sorting a file

On Saturday 14 June 2008 03:15, Beema
shafreen wrote:
Hi all,

I have a file with three columns i need
to sort the file with respect to the third
column. How do I do it uisng python. I
used Linux command to do this. Sort but i
not able to do it ? can any body ssuggest
me

I have used this method to solve similar
problems.

This is a consept of how to do what you want,
but you will have to work a little to get it
right.

You might try something like this;

Dict = {} ##create a dictionary

make a list of all column3 values

for loop colum3 values

Make these values the key in a dictionary
If the values are long, you can use the first
7 to 15 characters if you want.

use this key to equal all the values in the
other columns on the same row.

Dict[column3] = column1, column2, column3

once the dictionary is made

get the dictionary key
x = Dict.keys() ## get the keys from Dict
x.sort() # produce a sorted list of keys of
column3

Loop these sorted keys to extract from the
dictionary the values related to each
jim-on-linux
http://:inqvista.com





Jun 27 '08 #1
1 1278
On Jun 15, 2:00 am, jim-on-linux <inq1...@inqvista.comwrote:
On Saturday 14 June 2008 03:15, Beema

shafreen wrote:
Hi all,
I have a file with three columns i need
to sort the file with respect to the third
column. How do I do it uisng python. I
used Linux command to do this. Sort but i
not able to do it ? can any body ssuggest
me

I have used this method to solve similar
problems.

This is a consept of how to do what you want,
but you will have to work a little to get it
right.

You might try something like this;

Dict = {} ##create a dictionary

make a list of all column3 values
Ummm this appears somewhat baroque .... try this:

Assuming the OP has arranged to read his data into a list of lists
"filedata" such that filedata[-1][2] is the data in the 3rd column of
the last row. Further assume that the data has just been read with
e.g. the csv module so all data is still in text form and may need
some conversion before comparison (otherwise e.g. a salary of 100000
would appear before a salary of 99999).

Suppose the 3rd column is a salary, without '$' and ',' in it.

Now let's look at the manual for the sort method. It gives us 2
options: a cmp function and a key function, with a strong hint that
key is better. So for each row, what do we want on sort on? It's the
numerical value of the 3rd item:

def sort_key_func(row):
return float(row[2])

Then we can simply do:
filedata.sort(key=sort_key_func)

All that remains is to write the contents of filedata to the output
file.

HTH,
John
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Lad | last post: by
22 posts views Thread by mike | last post: by
20 posts views Thread by Xah Lee | last post: by
25 posts views Thread by Dan Stromberg | last post: by
7 posts views Thread by Kamal | last post: by
3 posts views Thread by SneakyElf | last post: by
1 post views Thread by =?Utf-8?B?YmJkb2J1ZGR5?= | last post: by
5 posts views Thread by jrod11 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.