473,388 Members | 1,408 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,388 software developers and data experts.

pythonic way to sort

hi
I have a file with columns delimited by '~' like this:

1SOME STRING ~ABC~12311232432D~20060401~00000000
2SOME STRING ~DEF~13534534543C~20060401~00000000
3SOME STRING ~ACD~14353453554G~20060401~00000000

......

What is the pythonic way to sort this type of structured text file?
Say i want to sort by 2nd column , ie ABC, ACD,DEF ? so that it becomes

1SOME STRING ~ABC~12311232432D~20060401~00000000
3SOME STRING ~ACD~14353453554G~20060401~00000000
2SOME STRING ~DEF~13534534543C~20060401~00000000
?
I know for a start, that i have to split on '~', then append all the
second columns into a list, then sort the list using sort(), but i am
stuck with how to get the rest of the corresponding columns after the
sort....

thanks...

May 4 '06 #1
3 1563
mi*******@hotmail.com wrote:
hi
I have a file with columns delimited by '~' like this:

1SOME STRING ~ABC~12311232432D~20060401~00000000
2SOME STRING ~DEF~13534534543C~20060401~00000000
3SOME STRING ~ACD~14353453554G~20060401~00000000

.....

What is the pythonic way to sort this type of structured text file?
Say i want to sort by 2nd column , ie ABC, ACD,DEF ? so that it becomes

1SOME STRING ~ABC~12311232432D~20060401~00000000
3SOME STRING ~ACD~14353453554G~20060401~00000000
2SOME STRING ~DEF~13534534543C~20060401~00000000
?
I know for a start, that i have to split on '~', then append all the
second columns into a list, then sort the list using sort(), but i am
stuck with how to get the rest of the corresponding columns after the
sort....


In Python 2.4 and up, you can use the key= keyword to list.sort(). E.g.

In [2]: text = """1SOME STRING ~ABC~12311232432D~20060401~00000000
...: 2SOME STRING ~DEF~13534534543C~20060401~00000000
...: 3SOME STRING ~ACD~14353453554G~20060401~00000000"""

In [3]: lines = text.split('\n')

In [4]: lines
Out[4]:
['1SOME STRING ~ABC~12311232432D~20060401~00000000',
'2SOME STRING ~DEF~13534534543C~20060401~00000000',
'3SOME STRING ~ACD~14353453554G~20060401~00000000']

In [5]: lines.sort(key=lambda x: x.split('~')[1])

In [6]: lines
Out[6]:
['1SOME STRING ~ABC~12311232432D~20060401~00000000',
'3SOME STRING ~ACD~14353453554G~20060401~00000000',
'2SOME STRING ~DEF~13534534543C~20060401~00000000']

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

May 4 '06 #2

On May 4, 2006, at 12:12 AM, mi*******@hotmail.com wrote:
hi
I have a file with columns delimited by '~' like this:

1SOME STRING ~ABC~12311232432D~20060401~00000000
2SOME STRING ~DEF~13534534543C~20060401~00000000
3SOME STRING ~ACD~14353453554G~20060401~00000000

.....

What is the pythonic way to sort this type of structured text file?
Say i want to sort by 2nd column , ie ABC, ACD,DEF ? so that it becomes

1SOME STRING ~ABC~12311232432D~20060401~00000000
3SOME STRING ~ACD~14353453554G~20060401~00000000
2SOME STRING ~DEF~13534534543C~20060401~00000000
?
I know for a start, that i have to split on '~', then append all the
second columns into a list, then sort the list using sort(), but i am
stuck with how to get the rest of the corresponding columns after the
sort....

thanks...


A couple ways. Assume that you have the lines in a list called 'lines',
as follows:

lines = [
"1SOME STRING ~ABC~12311232432D~20060401~00000000",
"3SOME STRING ~ACD~14353453554G~20060401~00000000",
"2SOME STRING ~DEF~13534534543C~20060401~00000000"]
The more traditional way would be to define your own comparison
function:

def my_cmp(x,y):
return cmp( x.split("~")[1], y.split("~")[1])

lines.sort(cmp=my_cmp)
The newer, faster way, would be to define your own key function:

def my_key(x):
return x.split("~")[1]

lines.sort(key=my_key)
The key function is faster because you only have to do the
split("~")[1] once for each line, whereas it will be done many times
for each line if you use a comparison function.

Jay P.

May 4 '06 #3
Jay Parlar wrote:

On May 4, 2006, at 12:12 AM, mi*******@hotmail.com wrote:
[...] Assume that you have the lines in a list called 'lines',
as follows:

lines = [
"1SOME STRING ~ABC~12311232432D~20060401~00000000",
"3SOME STRING ~ACD~14353453554G~20060401~00000000",
"2SOME STRING ~DEF~13534534543C~20060401~00000000"]
The more traditional way would be to define your own comparison function:

def my_cmp(x,y):
return cmp( x.split("~")[1], y.split("~")[1])

lines.sort(cmp=my_cmp)
The newer, faster way, would be to define your own key function:

def my_key(x):
return x.split("~")[1]

lines.sort(key=my_key)


and if the data is in a file rather than a list, you may write eg

lines = sorted(file("/path/tofile"),key=mike)

to create it sorted.
May 4 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Tom Evans | last post by:
My basic question: If I have a specific interface which I know is going to be implemented by a number of classes, but there is no implementation commonality between them, what is the preferred...
10
by: Bulba! | last post by:
Hello everyone, I'm reading the rows from a CSV file. csv.DictReader puts those rows into dictionaries. The actual files contain old and new translations of software strings. The dictionary...
11
by: Charles Krug | last post by:
I've a function that needs to maintain an ordered sequence between calls. In C or C++, I'd declare the pointer (or collection object) static at the function scope. What's the Pythonic way to...
6
by: Sean Berry | last post by:
Hello all I have build a list that contains data in the form below -- simplified for question -- myList = ,, ...] I have a function which takes value3 from the lists above and returns...
2
by: Tony Nelson | last post by:
I'm looking for a "pythonic" GTK Undo library/class. It would have a framework for Undo/Redo, and would provide Undo/Redo for TextView, Entry, and containers and other classes. In a "batteries...
4
by: Carl J. Van Arsdall | last post by:
It seems the more I come to learn about Python as a langauge and the way its used I've come across several discussions where people discuss how to do things using an OO model and then how to design...
14
by: Pythor | last post by:
I wrote the following code for a personal project. I need a function that will plot a filled circle in a two dimensional array. I found Bresenham's algorithm, and produced this code. Please tell...
5
by: akameswaran | last post by:
Disclaimer - I recognize this is not a practical exercise. There are many implementations around that would do the job better, more efficiently (Meaning in C) or whatever. I caught some thread...
2
by: wink | last post by:
Hello, I would like to know what would be considered the most Pythonic way of handling errors when dealing with files, solutions that seem reasonable using 2.5: ------- try: f =...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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...

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.