[英]How to transform a dict array with multiple key values into a dict with single key value?
I have the following input data 我有以下输入数据
options_d = [{'id': 36, 'label': 'Angular'},
{'id': 37, 'label': 'Java'},
{'id': 38, 'label': 'PHP'},
{'id': 39, 'label': 'Python'},
{'id': 40, 'label': 'C#'},
{'id': 41, 'label': 'C'},
{'id': 42, 'label': '.NET'},
{'id': 43, 'label': 'Ruby'},
{'id': 44, 'label': 'Rails'},
{'id': 45, 'label': 'OS-Linux'},
{'id': 55, 'label': 'Maschinenbau'},
{'id': 56, 'label': 'Automotive'},
{'id': 57, 'label': 'Engineering'}]
And I would like to have it transformed into 我想把它变成
{36: 'Angular',
37: 'Java',
38: 'PHP',
39: 'Python',
40: 'C#',
41: 'C',
42: '.NET',
43: 'Ruby',
44: 'Rails',
45: 'OS-Linux',
55: 'Maschinenbau',
56: 'Automotive',
57: 'Engineering'}
What I did so far is 到目前为止我做了什么
skillsmap_person = {}
for option in options_d:
skillsmap_person[option['id']] = option['label']
It works. 有用。 However, is there a one-line dict assignment solution I can use? 但是,我可以使用单行dict分配解决方案吗?
Any ideas? 有任何想法吗?
Creating dictionary
with dict comprehensions
- 使用dict comprehensions
创建dictionary
-
options_dict = {i['id']:i['label'] for i in options_d}
print(options_dict)
{36: 'Angular',
37: 'Java',
38: 'PHP',
39: 'Python',
40: 'C#',
41: 'C',
42: '.NET',
43: 'Ruby',
44: 'Rails',
45: 'OS-Linux',
55: 'Maschinenbau',
56: 'Automotive',
57: 'Engineering'}
Why not doing dict
with list comprehension: 为什么不用列表理解做dict
:
>>> dict([i.values() for i in options_d])
{36: 'Angular', 37: 'Java', 38: 'PHP', 39: 'Python', 40: 'C#', 41: 'C', 42: '.NET', 43: 'Ruby', 44: 'Rails', 45: 'OS-Linux', 55: 'Maschinenbau', 56: 'Automotive', 57: 'Engineering'}
>>>
Or for lower versions (when dictionaries are unordered): 或者对于较低版本(当字典无序时):
>>> dict([list(i.values())[::-1] for i in options_d])
{36: 'Angular', 37: 'Java', 38: 'PHP', 39: 'Python', 40: 'C#', 41: 'C', 42: '.NET', 43: 'Ruby', 44: 'Rails', 45: 'OS-Linux', 55: 'Maschinenbau', 56: 'Automotive', 57: 'Engineering'}
>>>
It simple iterates through the list of dictionaries, and get's the values of the dictionary, then have an outer dict(...)
for making it a dictionary, so actually, odd indexed value would be keys, even indexed value would be values. 它简单地遍历字典列表,并得到字典的值,然后有一个外部dict(...)
使其成为字典,所以实际上,奇数索引值将是键,甚至索引值将是值。
options_d = [{'id': 36, 'label': 'Angular'},
{'id': 37, 'label': 'Java'},
{'id': 38, 'label': 'PHP'},
{'id': 39, 'label': 'Python'},
{'id': 40, 'label': 'C#'},
{'id': 41, 'label': 'C'},
{'id': 42, 'label': '.NET'},
{'id': 43, 'label': 'Ruby'},
{'id': 44, 'label': 'Rails'},
{'id': 45, 'label': 'OS-Linux'},
{'id': 55, 'label': 'Maschinenbau'},
{'id': 56, 'label': 'Automotive'},
{'id': 57, 'label': 'Engineering'}]
{elem['id'] : elem['label'] for elem in options_d}
gets you: 得到你:
{36: 'Angular',
37: 'Java',
38: 'PHP',
39: 'Python',
40: 'C#',
41: 'C',
42: '.NET',
43: 'Ruby',
44: 'Rails',
45: 'OS-Linux',
55: 'Maschinenbau',
56: 'Automotive',
57: 'Engineering'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.