Python元组从大到小排序的方法是什么

lewis 2017-09-13 23次阅读

使用sorted()函数以及reverse参数可以对元组进行从大到小的排序。

例如:

tup = (3, 1, 4, 2, 5)
sorted_tup = sorted(tup, reverse=True)
print(sorted_tup)

输出:

[5, 4, 3, 2, 1]

还可以使用内置的sorted()函数结合lambda表达式进行排序:

tup = (3, 1, 4, 2, 5)
sorted_tup = sorted(tup, key=lambda x: -x)
print(sorted_tup)

输出:

[5, 4, 3, 2, 1]


发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。