码迷,mamicode.com
首页 > 编程语言 > 详细

python的排序函数

时间:2018-03-24 17:28:50      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:strong   str   post   二维   rev   lambda   log   pos   ever   

1、sort和sorted

eg:

a = [5,2,1,9,6]

sorted(a)    #将a从小到大排序,不影响a本身结构

a.sort()     #将a从小到大排序,影响a本身结构

a.sort(reverse=True)    #将a从大到小排序,影响a本身结构

2、二维list按某列排序

lis = [[4, 2, 9], [1, 5, 6], [7, 8, 3]]
lis.sort(key=lambda k: k[2])

备注:k[2]即为第2列(编号从0开始)

python的排序函数

标签:strong   str   post   二维   rev   lambda   log   pos   ever   

原文地址:https://www.cnblogs.com/cgc0415/p/8639760.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!