By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,705 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,705 IT Pros & Developers. It's quick & easy.

Re: sorting a file

P: n/a
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
Share this Question
Share on Google+
1 Reply

P: n/a
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.