我花了一些时间阅读SOF并且遇到解决这个问题的问题。我似乎无法找到如何获得按子值排序的以下数据结构:
data = {}
data[1] = {name: "Bob", ...}
data[2] = {name: "Carl", ...}
data[3] = {nane: "Alice", ...}
我需要将这些数据转换为某种形式的list / tuple / order dict结构,该结构按字母顺序排列,以便最终结果如下:
finalData = [{name: "Alice", ...}, {name: "Bob", ...}, {name: "Carl", ...}]
感谢。
答案 0 :(得分:4)
你的意思是
sorted(data.values(), key=itemgetter(name))
>>> from operator import itemgetter
>>> data = {}
>>> name = 'name'
>>>
>>> data[1] = {name: "Bob"}
>>> data[2] = {name: "Carl"}
>>> data[3] = {name: "Alice"}
>>>
>>> sorted(data.values(), key=itemgetter(name))
[{'name': 'Alice'}, {'name': 'Bob'}, {'name': 'Carl'}]