📜  Python – 将字典列表转换为字典值列表

📅  最后修改于: 2022-05-13 01:55:29.432000             🧑  作者: Mango

Python – 将字典列表转换为字典值列表

给定一个字典列表,转换为具有相同键的字典,并将所有值映射为值列表。

方法#1:使用循环

这是解决这个问题的粗暴方法。在此,我们遍历所有字典,并提取每个键并在嵌套循环中转换为所需的字典。

Python3
# Python3 code to demonstrate working of 
# Convert list of dictionaries to Dictionary Value list
# Using loop
from collections import defaultdict
  
# initializing lists
test_list = [{"Gfg" : 6, "is" : 9, "best" : 10}, 
             {"Gfg" : 8, "is" : 11, "best" : 19},
             {"Gfg" : 2, "is" : 16, "best" : 10},
             {"Gfg" : 12, "is" : 1, "best" : 8},
             {"Gfg" : 22, "is" : 6, "best" : 8}]
  
# printing original list
print("The original list : " + str(test_list))
  
# using loop to get dictionaries
# defaultdict used to make default empty list 
# for each key
res = defaultdict(list)
for sub in test_list:
    for key in sub:
        res[key].append(sub[key])
      
# printing result 
print("The extracted dictionary : " + str(dict(res)))


Python3
# Python3 code to demonstrate working of 
# Convert list of dictionaries to Dictionary Value list
# Using list comprehension + dictionary comprehension
from collections import defaultdict
  
# initializing lists
test_list = [{"Gfg" : 6, "is" : 9, "best" : 10}, 
             {"Gfg" : 8, "is" : 11, "best" : 19},
             {"Gfg" : 2, "is" : 16, "best" : 10},
             {"Gfg" : 12, "is" : 1, "best" : 8},
             {"Gfg" : 22, "is" : 6, "best" : 8}]
  
# printing original list
print("The original list : " + str(test_list))
  
# dictionary and list comprehension 
# for shorthand to solution of problem
res = defaultdict(list)
{res[key].append(sub[key]) for sub in test_list for key in sub} 
      
# printing result 
print("The extracted dictionary : " + str(dict(res)))


输出
The original list : [{'Gfg': 6, 'is': 9, 'best': 10}, {'Gfg': 8, 'is': 11, 'best': 19}, {'Gfg': 2, 'is': 16, 'best': 10}, {'Gfg': 12, 'is': 1, 'best': 8}, {'Gfg': 22, 'is': 6, 'best': 8}]
The extracted dictionary : {'Gfg': [6, 8, 2, 12, 22], 'is': [9, 11, 16, 1, 6], 'best': [10, 19, 10, 8, 8]}

方法#2:使用列表理解+字典理解

上述功能的组合可以用来解决这个问题。在此,我们使用字典推导来构造字典,列表推导用于从原始列表中提取值。

Python3

# Python3 code to demonstrate working of 
# Convert list of dictionaries to Dictionary Value list
# Using list comprehension + dictionary comprehension
from collections import defaultdict
  
# initializing lists
test_list = [{"Gfg" : 6, "is" : 9, "best" : 10}, 
             {"Gfg" : 8, "is" : 11, "best" : 19},
             {"Gfg" : 2, "is" : 16, "best" : 10},
             {"Gfg" : 12, "is" : 1, "best" : 8},
             {"Gfg" : 22, "is" : 6, "best" : 8}]
  
# printing original list
print("The original list : " + str(test_list))
  
# dictionary and list comprehension 
# for shorthand to solution of problem
res = defaultdict(list)
{res[key].append(sub[key]) for sub in test_list for key in sub} 
      
# printing result 
print("The extracted dictionary : " + str(dict(res)))
输出
The original list : [{'Gfg': 6, 'is': 9, 'best': 10}, {'Gfg': 8, 'is': 11, 'best': 19}, {'Gfg': 2, 'is': 16, 'best': 10}, {'Gfg': 12, 'is': 1, 'best': 8}, {'Gfg': 22, 'is': 6, 'best': 8}]
The extracted dictionary : {'Gfg': [6, 8, 2, 12, 22], 'is': [9, 11, 16, 1, 6], 'best': [10, 19, 10, 8, 8]}