python數(shù)字降序排列怎么實(shí)現(xiàn)

小億
181
2023-12-04 18:38:02
欄目: 編程語言

可以使用sorted函數(shù)來實(shí)現(xiàn)數(shù)字的降序排列。代碼如下:

numbers = [5, 2, 9, 1, 7]
sorted_numbers = sorted(numbers, reverse=True)
print(sorted_numbers)

輸出結(jié)果為:

[9, 7, 5, 2, 1]

在sorted函數(shù)中,可以使用reverse=True參數(shù)來指定降序排列。如果不指定該參數(shù),默認(rèn)是升序排列。

0