python浮点数list排序

来源:互联网 发布:郭沁夺冠知乎 编辑:程序博客网 时间:2024/06/05 17:16
temp = [1.1,2.2,4.4,3.3]temp.sort(cmp=lambda x,y: cmp(float(x), float(y)), reverse = True)print temp

 输出:

[4.4,3.3,2.2,1.1]


如果某个字段为浮点数,例如 [ ( 'a', 1.1), ('b', 2.2 ), ('c', 3.3 ), ('d', 4.4)]

temp = [ ( 'a', 1.1), ('b', 2.2 ), ('c', 3.3 ), ('d', 4.4)]temp.sort(key = lambda x:x[1], cmp=lambda x,y: cmp(float(x), float(y)), reverse = True)print temp


原创粉丝点击