首页 > IT职业 > IT图书 > python中怎么排序数组
python中数组排序的方法:1、一维数组采用sort函数进行排序;2、多维数组利用sort函数结合lambda匿名函数进行排序。

1、数组排序
a = [3,1,4,6] a.sort() print(a)
执行结果:

2、多维数组排
a = [['5.1.5.40580', 29], ['5.0.8.14000', 11], ['5.0.8.14999', 59], ['5.1.4.37882', 4]] a.sort(key=lambda x:x[1],reverse=True) print(a)
key=lambda x:x[1]:按照数组索引1列进行排序,无此参数默认以0列进行排序
reverse=True:倒序,无此参数默认升序
执行结果:

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3306/viewspace-2832899/ ,如需转载,请注明出处,否则将追究法律责任。