我正在解决一个问题,其中我想使用列表将python dict平铺成如下所示的值
[
{'a': [1,2,3,4], 'b':[0,9,8], 'c': 'row1'},
{'x': [1,2,3,4], 'y':[0,9,8], 'z': 'row2'}
]
类似
[
{'a':1, 'b': 0, 'c': 'row1'},
{'a':1, 'b': 9, 'c': 'row1'},
{'a':1, 'b': 8, 'c': 'row1'},
{'a':2, 'b': 0, 'c': 'row1'},
{'a':2, 'b': 9, 'c': 'row1'},
{'a':2, 'b': 8, 'c': 'row1'},
{'a':3, 'b': 0, 'c': 'row1'},
{'a':3, 'b': 9, 'c': 'row1'},
{'a':3, 'b': 8, 'c': 'row1'},
{'a':4, 'b': 0, 'c': 'row1'},
{'a':4, 'b': 9, 'c': 'row1'},
{'a':4, 'b': 8, 'c': 'row1'},
{'x':1, 'y': 0, 'z': 'row2'},
{'x':1, 'y': 9, 'z': 'row2'},
{'x':1, 'y': 8, 'z': 'row2'},
{'x':2, 'y': 0, 'z': 'row2'},
{'x':2, 'y': 9, 'z': 'row2'},
{'x':2, 'y': 8, 'z': 'row2'},
{'x':3, 'y': 0, 'z': 'row2'},
{'x':3, 'y': 9, 'z': 'row2'},
{'x':3, 'y': 8, 'z': 'row2'},
{'x':4, 'y': 0, 'z': 'row2'},
{'x':4, 'y': 9, 'z': 'row2'},
{'x':4, 'y': 8, 'z': 'row2'},
]
我曾尝试使用熊猫来平整字典,但是没有什么建设性的内容。有点想找出替代方案
答案 0 :(得分:0)
您可以使用itertools.product
在dict项上生成笛卡尔积(根据变量l
中的输入列表):
from itertools import product
[dict(p) for d in l for p in product(*([(k, i) for i in v] if isinstance(v, list) else [(k, v)] for k, v in d.items()))]
这将返回:
[{'a': 1, 'b': 0, 'c': 'row1'},
{'a': 1, 'b': 9, 'c': 'row1'},
{'a': 1, 'b': 8, 'c': 'row1'},
{'a': 2, 'b': 0, 'c': 'row1'},
{'a': 2, 'b': 9, 'c': 'row1'},
{'a': 2, 'b': 8, 'c': 'row1'},
{'a': 3, 'b': 0, 'c': 'row1'},
{'a': 3, 'b': 9, 'c': 'row1'},
{'a': 3, 'b': 8, 'c': 'row1'},
{'a': 4, 'b': 0, 'c': 'row1'},
{'a': 4, 'b': 9, 'c': 'row1'},
{'a': 4, 'b': 8, 'c': 'row1'},
{'x': 1, 'y': 0, 'z': 'row2'},
{'x': 1, 'y': 9, 'z': 'row2'},
{'x': 1, 'y': 8, 'z': 'row2'},
{'x': 2, 'y': 0, 'z': 'row2'},
{'x': 2, 'y': 9, 'z': 'row2'},
{'x': 2, 'y': 8, 'z': 'row2'},
{'x': 3, 'y': 0, 'z': 'row2'},
{'x': 3, 'y': 9, 'z': 'row2'},
{'x': 3, 'y': 8, 'z': 'row2'},
{'x': 4, 'y': 0, 'z': 'row2'},
{'x': 4, 'y': 9, 'z': 'row2'},
{'x': 4, 'y': 8, 'z': 'row2'}]
答案 1 :(得分:0)
这是一个有效的实现,首先检查哪些字典条目是列表,使用itertools.product
从这些列表中计算值组合,然后为每个组合生成字典:
import itertools
input_list = [
{'a': [1,2,3,4], 'b':[0,9,8], 'c': 'row1'},
{'x': [1,2,3,4], 'y':[0,9,8], 'z': 'row2'}
]
output_list = []
for input_dict in input_list:
list_keys = [] # store keys with list value
for key,val in input_dict.items():
if isinstance(val,list):
list_keys.append(key)
# eg. for the 1st dictionary in input_list:
# list_keys = ['a','b']
product = list(itertools.product(*[input_dict[list_key] for list_key in list_keys]))
# eg. for the 1st dictionary in input_list:
# product = [(1, 0), (1, 9), (1, 8), (2, 0), (2, 9), (2, 8), (3, 0), (3, 9), (3, 8), (4, 0), (4, 9), (4, 8)]
# append a dictionary to the output list for each combination
for combination in product:
output_dict = input_dict.copy()
for i,e in enumerate(combination):
output_dict[list_keys[i]] = e
output_list.append(output_dict)
print(output_list)
输出:
[{'a': 1, 'b': 0, 'c': 'row1'},
{'a': 1, 'b': 9, 'c': 'row1'},
{'a': 1, 'b': 8, 'c': 'row1'},
{'a': 2, 'b': 0, 'c': 'row1'},
{'a': 2, 'b': 9, 'c': 'row1'},
{'a': 2, 'b': 8, 'c': 'row1'},
{'a': 3, 'b': 0, 'c': 'row1'},
{'a': 3, 'b': 9, 'c': 'row1'},
{'a': 3, 'b': 8, 'c': 'row1'},
{'a': 4, 'b': 0, 'c': 'row1'},
{'a': 4, 'b': 9, 'c': 'row1'},
{'a': 4, 'b': 8, 'c': 'row1'},
{'x': 1, 'y': 0, 'z': 'row2'},
{'x': 1, 'y': 9, 'z': 'row2'},
{'x': 1, 'y': 8, 'z': 'row2'},
{'x': 2, 'y': 0, 'z': 'row2'},
{'x': 2, 'y': 9, 'z': 'row2'},
{'x': 2, 'y': 8, 'z': 'row2'},
{'x': 3, 'y': 0, 'z': 'row2'},
{'x': 3, 'y': 9, 'z': 'row2'},
{'x': 3, 'y': 8, 'z': 'row2'},
{'x': 4, 'y': 0, 'z': 'row2'},
{'x': 4, 'y': 9, 'z': 'row2'},
{'x': 4, 'y': 8, 'z': 'row2'}]