要对Python中的数组进行大小排序,可以使用sort()方法或者sorted()函数。下面是两种方法的示例:
使用sort()方法:
arr = [3, 1, 5, 2, 4]
arr.sort()
print(arr)
使用sorted()函数:
arr = [3, 1, 5, 2, 4]
sorted_arr = sorted(arr)
print(sorted_arr)
这两种方法都会将数组中的元素按照从小到大的顺序进行排序。如果要对数组进行降序排序,可以传入reverse=True参数,如下所示:
arr = [3, 1, 5, 2, 4]
arr.sort(reverse=True)
print(arr)
sorted_arr = sorted(arr, reverse=True)
print(sorted_arr)
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: python数组中怎么查找某个值