Python中的UNIX样式排序? [英] UNIX-style sort in Python?

查看:48
本文介绍了Python中的UNIX样式排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

至少有一段时间我必须在Windows而不是UNIX中工作,这对于b $ b更为熟悉。我正在尝试用Python做一些我多年来在shell中做过的事情,特别是排序。 shell排序是

很容易使用:

%sort -t,+ 2 +5 imputfilename< return>


其中-t是字段分隔符,在本例中是逗号,而+2和

+4是要按顺序排序的字段。实际上,这些字段是从零开始的,所以第一个和第三个字段是排序的。


那么,是否有可用的模块或功能这样做了吗?


Lance

For a while at least I have to work in Windows rather than UNIX, which
is more familiar. I''m trying to do with Python some of the things that
I''ve done for years in shell, in particular, sort. The shell sort is
pretty easy to use:
% sort -t, +2 +5 imputfilename <return>

where -t is the field separator, in this case a comma, , and +2 and
+4 are the fields to be sorted, in that order. Actually, the fields are
zero-based, so the first and third fields would be the sorted.

So, is there a module or function already available that does this?

Lance

推荐答案

Kotlin Sam写道:
Kotlin Sam wrote:
%sort -t,+ 2 +5 imputfilename< return>
那么,是否有可用的模块或功能呢?
% sort -t, +2 +5 imputfilename <return> So, is there a module or function already available that does this?




在较新的Pythons(CVS和beta-1 for 2.4)中你可以做到


def get_fields(行):

fields = line.split(" \t")

返回字段[1 ],fields [4]


sorted_lines = sorted(open(" imputfilename"),key = get_fields)


对于年龄较大的蟒蛇你'我需要自己做装饰 - 排序 - 未装饰

(DSU),比如这个


lines = [get_fields( line),open in line(" imputfilename")]

lines.sort()

sorted_lines = [x [1] for x in lines]


这两者之间略有不同。如果字段[1]

和字段[4]在比较中的两行之间相同

那么这些排序中的第一个按每行的位置排序(它''' s

a" stable sort")而后者按

行的内容排序。


Andrew
da***@dalkescientific.com


2004-10- 18,Kotlin Sam< xa *********** @ hotmail.com>写道:
On 2004-10-18, Kotlin Sam <xa***********@hotmail.com> wrote:
至少有一段时间我必须在Windows而不是UNIX工作,而
更为熟悉。我正在尝试用Python做一些我多年来在shell中做过的事情,特别是排序。 shell排序很容易使用:
For a while at least I have to work in Windows rather than UNIX, which
is more familiar. I''m trying to do with Python some of the things that
I''ve done for years in shell, in particular, sort. The shell sort is
pretty easy to use:




听起来你需要安装Cygwin所以你有一个真正的bash

shell和所有正常的shell实用程序。


-

Grant Edwards grante哇!我在大西洋城

骑在舒适的

visi.com滚动椅......



Sounds like you need to install Cygwin so you have a real bash
shell and all of the normal shell utilities.

--
Grant Edwards grante Yow! I''m in ATLANTIC CITY
at riding in a comfortable
visi.com ROLLING CHAIR...


Andrew Dalke< ad **** @ mindspring.com>写道:
Andrew Dalke <ad****@mindspring.com> wrote:
Kotlin Sam写道:
Kotlin Sam wrote:
%sort -t,+ 2 +5 imputfilename< return>
% sort -t, +2 +5 imputfilename <return>
所以,有没有可用的模块或功能呢?
So, is there a module or function already available that does this?



在较新的Pythons(CVS和beta-1 for 2.4)中,你可以做到def get_fields(

) line):
fields = line.split(" \t")
返回字段[1],字段[4]

sorted_lines = sorted(打开(") imputfilename"),key = get_fields)



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)




非常正确 - 当然,如果Katlin需要get_fields来依赖

sys .argv参数很容易安排。


对于年龄较大的Pythons,你需要做decorate-sort-undecorate
(DSU ;)你自己,像这样

lines = [get_fields(line),line for line in open(" imputfilename")]


错误的语法 - 需要:


lines = [(get_fields(line),line)for open in line(" imputfilename")]

lines.sort()
sorted_lines = [x [1] for x in lines]

这两者之间略有不同。如果字段[1]
和字段[4]在比较中的两行之间相同
那么这些中的第一行按每行的位置排序(它是'
a"稳定排序")而后者按
行的内容排序。



Quite right -- and, of course, if Katlin needs get_fields to depend on
the sys.argv parameters that''s easy to arrange.

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")]
Wrong syntax -- needs to be:

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.




....并获得完全相同的稳定排序语义2.3,只需更改

三个陈述中的第一个陈述:


lines = [(get_fields(line),i,line)

for i,line in enumerate(open(" imputfilename"))]

Alex



....and to get exactly the same stable-sort semantics in 2.3, just change
the first one of the three statements to:

lines = [ (get_fields(line), i, line)
for i, line in enumerate(open("imputfilename")) ]
Alex


这篇关于Python中的UNIX样式排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆