如何在多个字段中对一堆列表进行排序? [英] how can I sort a bunch of lists over multiple fields?

查看:72
本文介绍了如何在多个字段中对一堆列表进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不认为这会像我现在看起来那么困难。


我正在读取一个csv文件,该文件记录了一些关于
大约200本书,例如标题,作者,出版商,isbn,日期和
其他一些信息。


我可以对第一个字段做一个简单的排序(事实证明标题),

,这样就可以了:

import string


bookData = open(r''D:\ path\to\books.csv'',''r'')

sBookData = bookData.read( )

lBooks = string.split(sBookData,''\ n'')


lBooks.sort()

sorted = string.join(lBooks,''\ n'')

output = open(r''D:\ path\to\output.csv'',''w '')

output.close()

我真的希望能够根据其他

标准对书籍列表进行排序,以及甚至多个标准(例如作者,然后通过

date。)


我使用python 2.4并找到了这个网站:

http://aspn.activestate.com/ASPN/Coo.../Recipe/ 305304


所以我尝试过像

I didn''t think this would be as difficult as it now seems to me.

I am reading in a csv file that documents a bunch of different info on
about 200 books, such as title, author, publisher, isbn, date and
several other bits of info too.

I can do a simple sort over the first field (title as it turns out),
and that is fine as far as it gets:
import string

bookData = open(r''D:\path\to\books.csv'', ''r'')
sBookData = bookData.read()
lBooks = string.split(sBookData, ''\n'')

lBooks.sort()
sorted = string.join(lBooks, ''\n'')
output = open(r''D:\path\to\output.csv'', ''w'')
output.close()
I really want to be able to sort the list of books based on other
criterium, and even multiple criteria (such as by author, and then by
date.)

I am using python 2.4 and have found this site:

http://aspn.activestate.com/ASPN/Coo.../Recipe/305304

and so I tried doing things like

lBooks.sort(cmp = cmp5)
Traceback(最近一次调用最后一次):

文件"<交互式输入>",第1行,在?

NameError:名称''cmp5''未定义


(我希望cmp5意味着它会使用列表中的第5项来

排序)

lBooks.sort(key = lambda i:i [4])
lBooks.sort(cmp=cmp5) Traceback (most recent call last):
File "<interactive input>", line 1, in ?
NameError: name ''cmp5'' is not defined

(I was hoping that cmp5 meant it would use the 5th item in the lists to
sort across)
lBooks.sort(key=lambda i:i[4])



Traceback(大多数)最近的电话最后一次):

文件"<互动输入>",第1行,在?

文件"<互动输入>",第1行,在< lambda& GT;

IndexError:字符串索引超出范围

(我希望类似的东西)

你会非常友好地指出我在正确的方向?


谢谢!


googleboy


Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 1, in <lambda>
IndexError: string index out of range
(I was hoping for similar things)
would you be so kind as to point me in the right direction?

THanks!

googleboy

推荐答案

我想如果你倒退了,它会出来:


a = [''bob'',''greg'',''cindy'', ''alice'']

b = [''fred'',barney'',''betty'',''wilma'',''pebbles'',''bambam'']

c = [''jed'',''奶奶'',''jethro'',''ellie-mae'']

d = [''bob' ',''carol'',''ted'',''alice'']


e = [a,b,c,d]


for ary in e:

print ary

e.sort(lambda x,y:cmp(x [1],y [1 ]))


for ary:e />
print ary

e.sort(lambda x,y :cmp(x [0],y [0]))


for ary i ne:

print ary


James


2005年4月27日星期三上午10:34,所以说googleboy :
I think if you work backwards, it comes out:

a = [''bob'', ''greg'', ''cindy'', ''alice'']
b = [''fred'',barney'',''betty'',''wilma'',''pebbles'',''bambam'']
c = [''jed'', ''granny'', ''jethro'', ''ellie-mae'']
d = [''bob'',''carol'',''ted'',''alice'']

e = [a,b,c,d]

for ary in e:
print ary

e.sort(lambda x,y:cmp(x[1],y[1]))

for ary in e:
print ary

e.sort(lambda x,y:cmp(x[0],y[0]))

for ary in e:
print ary

James

On Wednesday 27 April 2005 10:34 am, so sayeth googleboy:
我不认为这会像现在看起来那么困难。

我正在读一个记录了大量不同信息的csv文件
大约200本书,如标题,作者,出版商,isbn,日期和其他几个信息。

我可以对第一个字段做一个简单的排序(标题,因为它结果),
这是好的,因为它得到:

导入字符串

bookData = open(r''D:\ path \to\books.csv'',''r'')
sBookData = bookData.read()
lBooks = string.split(sBookData,''\ n'')

lBooks.sort()
sorted = string.join(lBooks,''\ n'')
output = open(r''D:\ path \ to\output.csv'',''w'')
output.close()

我真的希望能够对列表进行排序基于其他
标准的书籍,甚至是多个标准(例如作者,然后是
日期。)

我正在使用python 2.4并找到了这个网站:

http:// aspn。 activestate.com/ASPN/Coo.../Recipe/305304

所以我尝试过像
I didn''t think this would be as difficult as it now seems to me.

I am reading in a csv file that documents a bunch of different info on
about 200 books, such as title, author, publisher, isbn, date and
several other bits of info too.

I can do a simple sort over the first field (title as it turns out),
and that is fine as far as it gets:
import string

bookData = open(r''D:\path\to\books.csv'', ''r'')
sBookData = bookData.read()
lBooks = string.split(sBookData, ''\n'')

lBooks.sort()
sorted = string.join(lBooks, ''\n'')
output = open(r''D:\path\to\output.csv'', ''w'')
output.close()
I really want to be able to sort the list of books based on other
criterium, and even multiple criteria (such as by author, and then by
date.)

I am using python 2.4 and have found this site:

http://aspn.activestate.com/ASPN/Coo.../Recipe/305304

and so I tried doing things like
lBooks.sort(cmp = cmp5)
Traceback(最近一次调用最后一次):
文件"< interactive input>",第1行,在?
NameError:name ''cmp5''没有定义

(我希望cmp5意味着它会使用列表中的第5项来排序)
lBooks.sort(key = lambda i:i [4])
lBooks.sort(cmp=cmp5)
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
NameError: name ''cmp5'' is not defined

(I was hoping that cmp5 meant it would use the 5th item in the lists to
sort across)
lBooks.sort(key=lambda i:i[4])



Traceback(最近一次呼叫最后一次):
文件"< interactive input>",第1行, in?
文件"< interactive input>",第1行,< lambda> IndexError:字符串索引超出范围

(我希望类似的东西)

你会非常友好地指出我正确的方向吗?

谢谢!

googleboy



Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 1, in <lambda>
IndexError: string index out of range
(I was hoping for similar things)
would you be so kind as to point me in the right direction?

THanks!

googleboy




-

James Stroud

加州大学洛杉矶分校基因组学和蛋白质组学研究所

专栏951570

洛杉矶,加利福尼亚州90095

http://www.jamesstroud.com/


你是什么我希望我想先将文件的所有行首先读入一个

字符串,然后让split方法根据

新行分割它 - 参见示例下面。


然后再次使用拆分将一行的所有元素放入另一行

列表 - 用逗号分隔。

现在您可以为要排序的所有列定义sortfunctions,例如

如下所示 - 并使用它们来比较元素。你得到一个类似的脚本:

- #!/ usr / bin / env python

-

-def cmp_index(a,b,ndx ):

- 如果[ndx]< b [ndx]:

- 返回-1

- elif a [ndx]> b [ndx]:

- 返回1

- 否则:

- 返回0

-

-def cmp_0(a,b):

- 返回cmp_index(a,b,0)

-

- def cmp_1(a,b):

- 返回cmp_index(a,b,1)

-

-s =''Kikker en Eend,Max Veldhuis \ nDikkie Dik,Jet Boeke \\\
Ruminations on

C ++,Andrew Koenig& Barbara Moo''

-s = s.split(''\ n'')

-l = []

-for我在s:

- l.append(i.split('',''))

-

-l.sort( cmp_0)

-print l

-l.sort(cmp_1)

-print l


输出如:

martin @ ubuntu:〜
What you want I guess is to read first all lines of the file into a
string as you did, and then let the split method split it based on
newlines only - see example below.

Then you use split again to put all elements of one line into another
list - split it on commas.

Now you can define sortfunctions for all columns you want to sort, e.g.
like below - and use those to compare elements. You get a script like:
-#!/usr/bin/env python
-
-def cmp_index(a, b, ndx):
- if a[ndx] < b[ndx]:
- return -1
- elif a[ndx] > b[ndx]:
- return 1
- else:
- return 0
-
-def cmp_0(a, b):
- return cmp_index(a, b, 0)
-
-def cmp_1(a, b):
- return cmp_index(a, b, 1)
-
-s = ''Kikker en Eend,Max Veldhuis\nDikkie Dik,Jet Boeke\nRuminations on
C++,Andrew Koenig & Barbara Moo''
-s = s.split(''\n'')
-l = []
-for i in s:
- l.append(i.split('',''))
-
-l.sort(cmp_0)
-print l
-l.sort(cmp_1)
-print l

with output like:
martin@ubuntu:~


./ test.py

[[''Dikkie Dik'' ,''Jet Boeke'',[''Kikker en Eend'',''Max Veldhuis'',

[''Ruminations on C ++'',''Andrew Koenig& Barbara Moo'']

[[''反思C ++',''Andrew Koenig& Barbara Moo'',[''Dikkie Dik'',

''Jet Boeke'',[''Kikker en Eend'',''Max Veldhuis'']
martin @ ubuntu:〜
./test.py
[[''Dikkie Dik'', ''Jet Boeke''], [''Kikker en Eend'', ''Max Veldhuis''],
[''Ruminations on C++'', ''Andrew Koenig & Barbara Moo'']]
[[''Ruminations on C++'', ''Andrew Koenig & Barbara Moo''], [''Dikkie Dik'',
''Jet Boeke''], [''Kikker en Eend'', ''Max Veldhuis'']]
martin@ubuntu:~


这篇关于如何在多个字段中对一堆列表进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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