Python——按最大行元素对矩阵进行排序
当需要按最大行元素对矩阵进行排序时,定义一个方法,该方法采用一个参数并使用“max”方法来确定结果。
示例
以下是对此方法的演示:
def sort_max(row):
return max(row)
my_list = [[15, 27, 18], [39, 20, 13], [13, 15, 56], [43, 13, 25]]
print("The list is :")
print(my_list)
my_list.sort(key = sort_max, reverse = True)
print("The result is :")
print(my_list)输出
The list is : [[15, 27, 18], [39, 20, 13], [13, 15, 56], [43, 13, 25]] The result is : [[13, 15, 56], [43, 13, 25], [39, 20, 13], [15, 27, 18]]
说明
定义了一个名为“sort_max”的方法,该方法采用行作为参数,并返回行的最大元素作为输出。
在方法外部,定义一个列表并在控制台上显示。
使用“sort”方法对列表进行排序,并将键指定为先前定义的方法。
此外,“sort”方法中的“reverse”参数设置为“True”,以便对列表进行逆序排序。
这是在控制台上显示的输出。
广告
数据结构
网络
关系型数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言
C++
C#
MongoDB
MySQL
Javascript
PHP