关键词list.sort()
people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
people.sort()
print(people)
> [[4, 4], [5, 0], [5, 2], [6, 1], [7, 0], [7, 1]]
people.sort(reverse=True)
print(people)
> [[7, 1], [7, 0], [6, 1], [5, 2], [5, 0], [4, 4]]
people.sort(key=lambda x:(-x[0],x[1]))
> [[7, 0], [7, 1], [6, 1], [5, 0], [5, 2], [4, 4]]
people.sort(key=lambda x:(x[0],-x[1]),reverse=True)
> [[7, 0], [7, 1], [6, 1], [5, 0], [5, 2], [4, 4]]
people = [1,5,6,-6,4,-2,-6,-2,-9,15]
people.sort(key=lambda x:abs(x))
print(people)
> [1, -2, -2, 4, 5, 6, -6, -6, -9, 15]
函数 sorted()
people = [1,5,6,-6,4,-2,-6,-2,-9,15]
sorted(people, key=lambda x:abs(x), reverse=True)
print(people)
> [1, 5, 6, -6, 4, -2, -6, -2, -9, 15]
list2 = sorted(people, key=lambda x:abs(x), reverse=True)
print(list2)
> [15, -9, 6, -6, -6, 5, 4, -2, -2, 1]