【发布时间】:2017-09-22 21:35:26
【问题描述】:
我知道有很多关于此的问题,但我正在尝试按 hitrate 列对下面的 dict 进行排序。
data = {
'a': {'get': 1, 'hitrate': 1, 'set': 1},
'b': {'get': 4, 'hitrate': 20, 'set': 5},
'c': {'get': 3, 'hitrate': 4, 'set': 3}
}
我尝试了很多东西,最有希望的是下面的方法似乎出错了。
s = sorted(data, key=lambda x: int(x['hitrate']))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
TypeError: string indices must be integers, not str
我能得到一些帮助吗?
谢谢!
【问题讨论】:
标签: python sorting dictionary lambda