2

我需要对字典进行排序,其中键是由字符串表示的分数,需要按其数值排序:

IE:

exp_time = {"2":10, "1/2":5:, "2.5":11, "1/200":9, "15":3, "1/30":6}

结果应该是这样的(降序):

15
2.5
2
1/2
1/30
1/200
4

1 回答 1

13
>>> import fractions
>>> exp_time = {"2":10, "1/2":5, "2.5":11, "1/200":9, "15":3, "1/30":6}
>>> sorted(exp_time, key=fractions.Fraction, reverse=True)
['15', '2.5', '2', '1/2', '1/30', '1/200']
于 2012-09-27T22:34:28.377 回答