简体   繁体   English

Python排序2D清单

[英]Python sort 2D list

I have a list in python 3.6.5 which goes as follows 我在python 3.6.5中有一个列表如下

a_list = [['name1', 5, 6, 3, 7], ['name3', 2, 6, 3, 6], ['name3', 2, 10, 8, 5]]

So it's basically name, score1, score2 etc. 所以基本上是名称,score1,score2等。

I want to add up the scores and rank they names highest to lowest 我想将分数加起来并将它们从高到低排列

So far, I have 到目前为止,我有

a_list.sort(a_list, key=lambda x: x[not sure what to put here], reverse=True)

I need a way of adding up the numbers in the list without affecting the str field. 我需要一种在不影响str字段的情况下将列表中的数字相加的方法。

How can I achieve this? 我该如何实现?

This should work: 这应该工作:

a_list = [['name1', 5, 6, 3, 7], ['name3', 2, 6, 3, 6], ['name3', 2, 10, 8, 5]]
a_list.sort(key=lambda x: sum(x[1:]), reverse=True)
print(a_list)

Output: 输出:

[['name3', 2, 10, 8, 5], ['name1', 5, 6, 3, 7], ['name3', 2, 6, 3, 6]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM