排列多个子字典值字典的词典 [英] Sort Dictionary of Dictionaries on multiple child dictionary values
问题描述
我有一个字典,如下所示:
I have a dictionary that looks like this:
myDict = {
'SER12346': {'serial_num': 'SER12346', 'site_location': 'North America'},
'ABC12345': {'serial_num': 'ABC12345', 'site_location': 'South America'},
'SER12345': {'serial_num': 'SER12345', 'site_location': 'North America'},
'SER12347': {'serial_num': 'SER12347', 'site_location': 'South America'},
'ABC12346': {'serial_num': 'ABC12346', 'site_location': 'Europe'}
}
我的目标是通过 site_location
和每个孩子的 serial_num
对这个字典进行排序字典。
My goal is to sort this dictionary by the site_location
and the serial_num
of each of the child dictionaries.
使用我在这个问题中找到的代码 - 排序字典python的字典 - 我能够把它排序,但这不是我期待的。
Using code I found in this question - Sort a dictionary of dictionaries python - I was able to get it sorted, but it's not quite what I am expecting.
这是我的代码:
import pprint
items = ((k, k2, v) for k in myDict for k2, v in myDict[k].items())
ordered = sorted(items, key=lambda x:x[-1], reverse=False)
pprint.pprint(ordered)
这是我得到的结果: / p>
This is the result I get:
[('ABC12346', 'site_location', 'Europe'),
('SER12345', 'site_location', 'North America'),
('SER12346', 'site_location', 'North America'),
('SER12347', 'site_location', 'South America'),
('ABC12345', 'site_location', 'South America'),
('ABC12346', 'serial_num': 'ABC12346'),
('SER12345', 'serial_num': 'SER12345'),
('SER12346', 'serial_num': 'SER12346'),
('SER12347', 'serial_num': 'SER12347'),
('ABC12345', 'serial_num': 'ABC12345')]
我期待着某种东西e如下:
I was expecting something more like this though:
{
'ABC12346': {'serial_num': 'ABC12346', 'site_location': 'Europe'}
'SER12345': {'serial_num': 'SER12345', 'site_location': 'North America'},
'SER12346': {'serial_num': 'SER12346', 'site_location': 'North America'},
'SER12347': {'serial_num': 'SER12347', 'site_location': 'South America'},
'ABC12345': {'serial_num': 'ABC12345', 'site_location': 'South America'},
}
实际结果正在分离序列号和站点位置。我想把它们放在一起排序的对象。如何做到这一点?
The actual result is separating the serial number and site location. I want to keep them together in the sorted object. How can I do this?
推荐答案
这是你想要的吗?
dicts = [{k: v} for (k,v) in myDict.items()]
dicts.sort(key=lambda d: (d.values()[0]['site_location'], d.values()[0]['serial_num'],))
输出:
import pprint
pprint.pprint(dicts)
是:
[{'ABC12346': {'serial_num': 'ABC12346', 'site_location': 'Europe'}},
{'SER12345': {'serial_num': 'SER12345', 'site_location': 'North America'}},
{'SER12346': {'serial_num': 'SER12346', 'site_location': 'North America'}},
{'ABC12345': {'serial_num': 'ABC12345', 'site_location': 'South America'}},
{'SER12347': {'serial_num': 'SER12347', 'site_location': 'South America'}}]
编辑:我去了对于输出格式的答案,但这可能会更有意义:
I was going on your answer for the output format, but this would probably make more sense:
dicts = myDict.items()
dicts.sort(key=lambda (k,d): (d['site_location'], d['serial_num'],))
输出:
[('ABC12346', {'serial_num': 'ABC12346', 'site_location': 'Europe'}),
('SER12345', {'serial_num': 'SER12345', 'site_location': 'North America'}),
('SER12346', {'serial_num': 'SER12346', 'site_location': 'North America'}),
('ABC12345', {'serial_num': 'ABC12345', 'site_location': 'South America'}),
('SER12347', {'serial_num': 'SER12347', 'site_location': 'South America'})]
这篇关于排列多个子字典值字典的词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!