To sort a list in descending order in Python, you can use the sort() method with the reverse=True parameter or use the sorted() function with the reverse=True parameter. Here's an example:

Using the sort() method:

my_list = [5, 2, 8, 3, 1]
my_list.sort(reverse=True)
print(my_list)

Output:

[8, 5, 3, 2, 1]

Using the sorted() function:

my_list = [5, 2, 8, 3, 1]
sorted_list = sorted(my_list, reverse=True)
print(sorted_list)

Output:

[8, 5, 3, 2, 1]
Python List Sorting: Descending Order (With Examples)

原文地址: https://www.cveoy.top/t/topic/qbpL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录