473,785 Members | 2,879 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1505
On Jun 15, 2:00 am, jim-on-linux <inq1...@inqvis ta.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(r ow):
return float(row[2])

Then we can simply do:
filedata.sort(k ey=sort_key_fun c)

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
10899
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 somehow adding filename and date created in an array, sorting by date and then printing onto an html page with a link to that file. How would I do this though? Is this the simplest method or is there a better way of doin this?
5
1422
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 semicolons).
22
4171
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) { return -1; } else
20
4081
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: li=;
25
2232
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 function that will merge like elements into a chain, so that they won't have to be recompared as many times? Thanks!
7
4827
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 expandable row, the hidden row is made visible with css. The problem is when i sort the rows, the hidden rows get sorted as well which i don't want and want to be moved (while sorting) relative to their parent rows. The following is my complete html code...
3
2193
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 ( ; separates tv show and network) sort the strings according to the network and according to the show. and so: //to open file - ifstream myFile;
1
4162
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 info to a text file to see what is happening. What I found out is that when I clicked on a column to sort it the Sorting function was continuously being called thus causing the Default Application Pool in IIS to stop. Here is the code I am using
3
7347
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 uses unfamiliar terms and syntax. Intermediate and advanced Perl coders should find this article useful. The object of the article is to inform the reader, it is not about how to code Perl or how to write good Perl code, but to teach the Schwartzian...
5
4957
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 there are, it crashes. There are currently 6 columns, and I only want 4. How do I remove the last two (discount and date)? Here is a link: http://www.jaredmoore.com/tablesorter/docs/salestable.html Here is some jquery js that I think...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10329
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.