如何按嵌套列表中名称的字母顺序对带有嵌套列表的列表进行排序,然后按 Python 中的最大值对嵌套列表进行排序

How to sort list with nested lists alphabetically by the name in the nested lists, then sort the nested list by highest value in Python

我想知道如何按嵌套列表中名称字符串的字母顺序对分数列表进行排序,然后按最高值到最低值对嵌套列表中的分数进行排序:

例如:

list=[['a', 9, 3], ['we', 2], ['will', 2, 10], ['x', 4], ['z', 4]]

我试过使用 .sort()sorted() 以及一些 lambda,但我没有完全理解它。

预期输出为:

[['a', 9, 3],['we', 2],['will', 10, 2],['x',4],['z', 4]]    

好吧,在您的情况下,您想要对没有第一个元素的内部列表进行排序。您可以使用切片运算符来获取此子列表并替换它:

mylist=[['a', 9, 3], ['we', 2], ['will', 2, 10], ['x', 4], ['z', 4]]
for sublist in mylist:
    sublist[1:] = sorted(sublist[1:], reverse=True)

请注意,最好不要命名您的列表 list,因为它也是类型的名称。