python怎么将数字从小到大排列

442
2024/2/9 10:33:29
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

你可以使用sorted()函数对数字列表进行排序,如下所示:

numbers = [5, 2, 8, 1, 9]
sorted_numbers = sorted(numbers)
print(sorted_numbers)

输出:

[1, 2, 5, 8, 9]

或者你可以使用列表的sort()方法,如下所示:

numbers = [5, 2, 8, 1, 9]
numbers.sort()
print(numbers)

输出:

[1, 2, 5, 8, 9]

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: python实现快速排序的方法是什么