[英]Python3 Dictionary Comprehension
I'm having difficulty working out a dictionary comprehension.我很难理解字典。
I have a list of dictionaries, where each dictionary contains identical keys with different values:我有一个字典列表,其中每个字典包含具有不同值的相同键:
list_of_dictionaries = [{k1:v1, k2:v2}{k1:v3, k2:v4}{k1:v5, k2:v6}, ...]
I would like to have a single dictionary of lists, where each key has a value which is a list of those values found under that key in the list of dictionaries:我想要一个列表字典,其中每个键都有一个值,该值是在字典列表中该键下找到的那些值的列表:
dictionary_of_lists = {k1:[v1,v3,v5], k2:[v2,v4,v6], ...}
At the moment I'm creating this single, consolidated dictionary by manually entering the keys and using a list comprehension to fetch the values:目前,我正在通过手动输入键并使用列表推导来获取值来创建这个单一的综合字典:
dictionary_of_lists = {
k1:[i[k1] for i in list_of_dictionaries],
k2:[i[k2] for i in list_of_dictionaries],
...
}
It's not so bad with a few keys, but with over twenty it's looking messy with repeated code.几个键还不错,但是超过二十个键看起来很乱,重复的代码。 I'm struggling to formulate a dictionary comprehension which would achieve the same result.
我正在努力制定可以达到相同结果的字典理解。 Something like "for each dictionary in this list, add the values corresponding to each key to a list represented by the same key in another dictionary"?
类似于“对于此列表中的每个字典,将与每个键对应的值添加到由另一个字典中的相同键表示的列表中”? I've tried the dict.update() method, which won't allow me to add the values to a list - it erases and 'updates' the value already there instead.
我已经尝试过 dict.update() 方法,它不允许我将值添加到列表中 - 它会擦除并“更新”已经存在的值。
Note how each line only differs by the key used.请注意每行仅因使用的键而异。 This means that the keys should be iterated over:
这意味着密钥应该被迭代:
list_of_dictionaries = [{1: 2, 2: 3}, {1: 4, 2: 5}, {1: 6, 2: 7}]
# Only safe if you know there will always be at least one dictionary
keys = list_of_dictionaries[0].keys()
dictionary_of_lists = \
{k: [i[k] for i in list_of_dictionaries]
for k in keys} # A second level of iteration to automate what you were doing manually before
print(dictionary_of_lists)
>>> {1: [2, 4, 6], 2: [3, 5, 7]}
A simple for loop does the trick and it's efficient.一个简单的 for 循环就可以了,而且效率很高。
The solution provided iterates for each key over all the dictionaries in the list.提供的解决方案对列表中所有字典的每个键进行迭代。
>>> from collections import defaultdict
>>> res = defaultdict(list)
>>> for d in list_of_dictionaries:
... for k, v in d.items():
... res[k].append(v)
...
Use Python collections libs:使用 Python collections库:
from collections import defaultdict
list_of_dictionaries = [{'k1': 'v1', 'k2':'v2'},
{'k1':'v3', 'k2':'v4'},
{'k1':'v5', 'k2':'v6'}]
res = defaultdict(list)
for element in list_of_dictionaries:
for key, value in element.items():
res[key].append(value)
print(dict(res))
out: {'k1': ['v1', 'v3', 'v5'], 'k2': ['v2', 'v4', 'v6']}
If you are allowed to use pandas, this is a much simpler solution.如果允许您使用 pandas,这是一个更简单的解决方案。
Using pandas, here's what you will get:使用 pandas,您将得到:
import pandas as pd
list_of_dicts = [{'k1':'v1', 'k2':'v2'}, {'k1':'v3', 'k2':'v4'},
{'k1':'v5', 'k2':'v6'}, {'k1':'v7', 'k2':'v8'},
{'k1':'v9', 'k2':'v10'}]
df = pd.DataFrame(list_of_dicts)
k = {c:df[c].tolist() for c in df.columns}
print (k)
The output of this will be: output 将是:
{'k1': ['v1', 'v3', 'v5', 'v7', 'v9'], 'k2': ['v2', 'v4', 'v6', 'v8', 'v10']}
With this approach, you can keep adding as many keys as you like, the solution will be same.使用这种方法,您可以继续添加任意数量的键,解决方案将是相同的。
import pandas as pd
list_of_dicts = [{'k1':'v1' , 'k2':'v2' , 'k3': 'v3'},
{'k1':'v4' , 'k2':'v5' , 'k3': 'v6'},
{'k1':'v7' , 'k2':'v8' , 'k3': 'v9'},
{'k1':'v10', 'k2':'v11', 'k3': 'v12'},
{'k1':'v13' ,'k2':'v14', 'k3': 'v15'}]
df = pd.DataFrame(list_of_dicts)
k = {c:df[c].tolist() for c in df.columns}
print (k)
This will result in:这将导致:
{'k1': ['v1', 'v4', 'v7', 'v10', 'v13'], 'k2': ['v2', 'v5', 'v8', 'v11', 'v14'], 'k3': ['v3', 'v6', 'v9', 'v12', 'v15']}
The only limitation is that each set of dicts have to have same number of elements (k1, k2, k3).唯一的限制是每组 dicts 必须具有相同数量的元素(k1、k2、k3)。 You cannot have (k1,k2) and (k1,k2,k3).
您不能拥有 (k1,k2) 和 (k1,k2,k3)。 Then the code will break as the dataframe is looking for same number of elements per column.
然后代码将中断,因为 dataframe 正在寻找每列相同数量的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.