📌  相关文章
📜  如何在python代码示例中对列表列表进行排序

📅  最后修改于: 2022-03-11 14:46:04.983000             🧑  作者: Mango

代码示例5
# You can use a lambda function, but you'll have to specify the index of the sorting key.

A = [[100, 'Yes'], [40, 'Maybe'], [60, 'No']]
print("Sorted List A based on index 0: % s" % (sorted(A, key=lambda x:x[0])))
B = [[2, 'Dog'], [0, 'Bird'], [7, 'Cat']]
print("Sorted List A based on index 1: % s" % (sorted(B, key=lambda x:x[1])))

# Also, you can use .sort() if you want to sort just by the first index

A = [[55, 90], [45, 89], [90, 70]]
A.sort()
print("New sorted list A is % s" % (A))
A.sort(reverse=True)
print("New reverse sorted list A is % s" % (A))

# You can even change the key sort, if you want to sort by length for example:

A = [[5, 90, 'Hi', 66], [80, 99], [56, 32, 80]]
A.sort(key=len) # <-
print("New sorted list A is % s" % (A))