Python 程序按降序对数组元素进行排序
当需要按降序对数组元素进行排序时,可以通过指定一个名为“reverse”的参数为 True 来使用“sort”方法。
以下是同样的演示 −
例
my_list = [44, 56, 42, 31, 11, 23, 78, 89, 9, 0] print("The list is :") print(my_list) my_list.sort(reverse = True) print("The list after sorting is :") print(my_list)
输出
The list is : [44, 56, 42, 31, 11, 23, 78, 89, 9, 0] The list after sorting is : [89, 78, 56, 44, 42, 31, 23, 11, 9, 0]
Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.
解释
定义了一个列表,并将其显示在控制台上。
在列表上调用“sort”方法。
此处将名为“reverse”的参数设置为“True”。
这有助于按降序显示元素。
输出显示在控制台上。
广告