UNIX-style sort in Python?
Andrew Dalke
adalke at mindspring.com
Sun Oct 17 20:42:46 EDT 2004
Kotlin Sam wrote:
> % sort -t, +2 +5 imputfilename <return>
> So, is there a module or function already available that does this?
In newer Pythons (CVS and beta-1 for 2.4) you can do
def get_fields(line):
fields = line.split("\t")
return fields[1], fields[4]
sorted_lines = sorted(open("imputfilename"), key=get_fields)
For older Pythons you'll need to do the "decorate-sort-undecorate"
("DSU") yourself, like this
lines = [get_fields(line), line for line in open("imputfilename")]
lines.sort()
sorted_lines = [x[1] for x in lines]
There is a slight difference between these two. If fields[1]
and fields[4] are the same between two lines in the comparison
then the first of these sorts by position of each line (it's
a "stable sort") while the latter sorts by the content of the
line.
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list