How to sort a list of dictionaries of dictionaries by value
This question already has an answer here:
You can by using a lambda
function as a key
argument to the function
>>> mydic = [{'name':'a', 'loc':{'locname':'z'}, 'id':{'value':'1'}},{'name':'b', 'loc':{'locname':'o'}, 'id':{'value':'2'}}]
>>> sorted(mydic,key = lambda x:x['loc']['locname'])
[{'loc': {'locname': 'o'}, 'name': 'b', 'id': {'value': '2'}}, {'loc': {'locname': 'z'}, 'name': 'a', 'id': {'value': '1'}}]
Ref: The docs
sorted(iterable[, cmp[, key[, reverse]]])
key
specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower
. The default value is None
(compare the elements directly).
上一篇: 按财产顺序重复列出所有的字典
下一篇: 如何根据价值对字典词典列表进行排序