我有一个从排序的csv创建的以下列表
list1 = sorted(csv1, key=operator.itemgetter(1))
Run Code Online (Sandbox Code Playgroud)
我实际上想按两个标准对列表进行排序:首先是字段1中的值,然后是字段2中的值.我该怎么做?
jaa*_*aap 287
回复此死线程进行存档.
使用lambda函数时无需导入任何内容.
以下list按第一个元素排序,然后按第二个元素排序.
sorted(list, key=lambda x: (x[0], -x[1]))
Run Code Online (Sandbox Code Playgroud)
mou*_*uad 147
像这样:
import operator
list1 = sorted(csv1, key=operator.itemgetter(1, 2))
Run Code Online (Sandbox Code Playgroud)
Dun*_*can 18
Python具有稳定的排序,因此只要性能不是问题,最简单的方法是按字段2对其进行排序,然后再按字段1对其进行排序.
这将给你你想要的结果,唯一的问题是如果它是一个大的列表(或者你想经常排序)调用sort两次可能是一个不可接受的开销.
list1 = sorted(csv1, key=operator.itemgetter(2))
list1 = sorted(list1, key=operator.itemgetter(1))
Run Code Online (Sandbox Code Playgroud)
这样做也可以很容易地处理您希望某些列反向排序的情况,只需在必要时包含'reverse = True'参数.
否则,您可以将多个参数传递给itemgetter或手动构建元组.这可能会更快,但有一个问题是,如果某些列想要反向排序它们不能很好地推广(数字列仍然可以通过否定它们来反转,但这会使排序保持稳定).
因此,如果您不需要任何反向排序的列,请为itemgetter添加多个参数(如果可能),并且列不是数字,或者您希望保持sort stable连续多个排序.
编辑:对于在理解如何回答原始问题时遇到问题的评论者,这里有一个示例,它确切地说明了排序的稳定性如何确保我们可以对每个键进行单独排序,最终得到按多个标准排序的数据:
DATA = [
('Jones', 'Jane', 58),
('Smith', 'Anne', 30),
('Jones', 'Fred', 30),
('Smith', 'John', 60),
('Smith', 'Fred', 30),
('Jones', 'Anne', 30),
('Smith', 'Jane', 58),
('Smith', 'Twin2', 3),
('Jones', 'John', 60),
('Smith', 'Twin1', 3),
('Jones', 'Twin1', 3),
('Jones', 'Twin2', 3)
]
# Sort by Surname, Age DESCENDING, Firstname
print("Initial data in random order")
for d in DATA:
print("{:10s} {:10s} {}".format(*d))
print('''
First we sort by first name, after this pass all
Twin1 come before Twin2 and Anne comes before Fred''')
DATA.sort(key=lambda row: row[1])
for d in DATA:
print("{:10s} {:10s} {}".format(*d))
print('''
Second pass: sort by age in descending order.
Note that after this pass rows are sorted by age but
Twin1/Twin2 and Anne/Fred pairs are still in correct
firstname order.''')
DATA.sort(key=lambda row: row[2], reverse=True)
for d in DATA:
print("{:10s} {:10s} {}".format(*d))
print('''
Final pass sorts the Jones from the Smiths.
Within each family members are sorted by age but equal
age members are sorted by first name.
''')
DATA.sort(key=lambda row: row[0])
for d in DATA:
print("{:10s} {:10s} {}".format(*d))
Run Code Online (Sandbox Code Playgroud)
这是一个可运行的示例,但为了节省运行它的人,输出是:
Initial data in random order
Jones Jane 58
Smith Anne 30
Jones Fred 30
Smith John 60
Smith Fred 30
Jones Anne 30
Smith Jane 58
Smith Twin2 3
Jones John 60
Smith Twin1 3
Jones Twin1 3
Jones Twin2 3
First we sort by first name, after this pass all
Twin1 come before Twin2 and Anne comes before Fred
Smith Anne 30
Jones Anne 30
Jones Fred 30
Smith Fred 30
Jones Jane 58
Smith Jane 58
Smith John 60
Jones John 60
Smith Twin1 3
Jones Twin1 3
Smith Twin2 3
Jones Twin2 3
Second pass: sort by age in descending order.
Note that after this pass rows are sorted by age but
Twin1/Twin2 and Anne/Fred pairs are still in correct
firstname order.
Smith John 60
Jones John 60
Jones Jane 58
Smith Jane 58
Smith Anne 30
Jones Anne 30
Jones Fred 30
Smith Fred 30
Smith Twin1 3
Jones Twin1 3
Smith Twin2 3
Jones Twin2 3
Final pass sorts the Jones from the Smiths.
Within each family members are sorted by age but equal
age members are sorted by first name.
Jones John 60
Jones Jane 58
Jones Anne 30
Jones Fred 30
Jones Twin1 3
Jones Twin2 3
Smith John 60
Smith Jane 58
Smith Anne 30
Smith Fred 30
Smith Twin1 3
Smith Twin2 3
Run Code Online (Sandbox Code Playgroud)
请特别注意,在第二步中,reverse=True参数如何按顺序保存第一个名称,而简单排序然后反转列表将失去第三个排序键的所需顺序.
def keyfunc(x):
return tuple(x[1],x[2])
list1 = sorted(csv1, key=keyfunc)
Run Code Online (Sandbox Code Playgroud)