473,396 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1484
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 easy way of doing this? I was thinking about...
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 also empty records such as ;;;; (only...
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); var date_b = new Date(b); if (date_a < date_b)...
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 Python, use the “sort” method. For example: ...
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, is there some sort of already-available sort...
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 (display:none). When one clicks in the left of the...
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 tv shows and their networks) one line at a time...
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 I can't get the sorting to work so I wrote some...
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 article. Beginning Perl coders may find this article...
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 remove code that I think controls how many colums...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

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.