python浮点数list排序

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


你可能感兴趣的:(python浮点数list排序)