排序函数(list / tuple)以相反的顺序错误地工作

时间:2016-08-29 18:47:51

标签: python python-2.7 sorting

fn = sorted(finalDataSet,key = lambda x : x[1],reverse=True)

我得到了这样的结果:

[('Hyperelastic', '8%'), ('Orthotropic', '26%'), ('Plastic', '23%'),
 ('Composites', '17%'), ('Anisotropic', '13%'), ('Thermal', '13%')]

1 个答案:

答案 0 :(得分:0)

它似乎工作正常。您按字符串排序,按字母顺序排序' 8%'被认为大于26%'您可能会发现这更有用lambda

lambda x : float(x[1].strip('%'))