[英]Dict from list of dicts
I have a list of dicts. 我有一个字典列表。
dictList = [
{'name': 'some name'},
{'name': 'some other name'},
{'age': 'some age'},
{'last_name': 'some last name'}
]
In that list of dicts each dict has one key and one value for that key, as shown above. 在该字典列表中,每个字典都有一个键和该键的一个值,如上所示。
I need to create a dict that has the keys from all the dicts and each value for every key is a set with item values from the list of dicts. 我需要创建一个包含所有dict中的键的dict,每个键的每个值都是一组带有dict列表中项目值的集合。 In the example, it'd be something like this:
在示例中,将是这样的:
expected_dict = {
'name': ['some name', 'some other name'],
'age': ['some age'],
'last_name': ['some last name']
}
How can I do this in Python? 如何在Python中执行此操作?
collections.defaultdict
is one way: collections.defaultdict
是一种方式:
from collections import defaultdict
d = defaultdict(list)
dictList = [
{'name': 'some name'},
{'name': 'some other name'},
{'age': 'some age'},
{'last_name': 'some last name'}
]
for i in dictList:
for k, v in i.items():
d[k].append(v)
# defaultdict(list,
# {'age': ['some age'],
# 'last_name': ['some last name'],
# 'name': ['some name', 'some other name']})
You can use the builtin setdefault()
function. 您可以使用内置的
setdefault()
函数。
dictList = [
{'name': 'some name'},
{'name': 'some other name'},
{'age': 'some age'},
{'last_name': 'some last name'}
]
expected_dict = {}
for dictionary in dictList:
for key, val in dictionary.items():
expected_dict.setdefault(key, []).append(val)
print(expected_dict)
Output: 输出:
{
'name': ['some name', 'some other name'],
'age': ['some age'],
'last_name': ['some last name']
}
Note: Using collections.defaultdict
(as shown in this answer ) is simpler and faster than using dict.setdefault()
. 注意:使用
collections.defaultdict
(如本答案所示)比使用dict.setdefault()
更简单,更快。
From the documentation : 从文档中 :
Working of collections.defaultdict
: collections.defaultdict
工作:
When each key is encountered for the first time, it is not already in the mapping;
首次遇到每个键时,它尚未在映射中; so an entry is automatically created using the
default_factory
function which returns an empty list.因此,将使用
default_factory
函数自动创建一个条目,该函数返回一个空列表。 Thelist.append()
operation then attaches the value to the new list.然后,
list.append()
操作list.append()
值附加到新列表。 When keys are encountered again, the look-up proceeds normally (returning the list for that key) and thelist.append()
operation adds another value to the list.当再次遇到键时,查找将正常进行(返回该键的列表),并且
list.append()
操作将另一个值添加到列表中。 This technique is simpler and faster than an equivalent technique usingdict.setdefault()
.与使用
dict.setdefault()
的等效技术相比,此技术更简单,更快。
bigD = {}
for element in dictList:
for key in element:
if key in bigD:
bigD[key].append(element[key])
else:
bigD[key] = element[key]
You can use itertools.groupby
: 您可以使用
itertools.groupby
:
import import itertools
dictList = [
{'name': 'some name'},
{'name': 'some other name'},
{'age': 'some age'},
{'last_name': 'some last name'}
]
new_list = {a:[c for [[_, c]] in b] for a, b in itertools.groupby(map(lambda x:x.items(), dictList), key=lambda x:x[0][0])}
Output: 输出:
{'age': ['some age'], 'last_name': ['some last name'], 'name': ['some name', 'some other name']}
Very simply. 很简单
dict = {}
list = [1,2,3]
dict['numbs'] = list
print(dict)
Output : {'numbs': [1, 2, 3]}
输出:
{'numbs': [1, 2, 3]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.