472,328 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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 1394
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: bissatch | last post by:
Hi, I am about to write an application that will display all pdf files in a folder and display them by file name in order of date. Is there an...
5
by: Lad | last post by:
Hi, I have a file of records of 4 fields each. Each field is separated by a semicolon. That is Filed1;Ffield2;Field3;Field4 But there may be...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a);...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in...
25
by: Dan Stromberg | last post by:
Hi folks. Python appears to have a good sort method, but when sorting array elements that are very large, and hence have very expensive compares,...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule...
3
by: SneakyElf | last post by:
i am very green with c++ so i get stuck on very simple things anyway, i need to write a program that would read data from file (containing names of...
1
by: =?Utf-8?B?YmJkb2J1ZGR5?= | last post by:
I have a grid view that pulls data from a dbf file. I set the Allow Sorting to true and I put my code in the Sorting event. The problem is that...
3
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.