Python Dict操作

2024-05-16 13:03:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理一个问题,我想用如下所示的值来平铺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'},
]

我试过用熊猫来压平口述,但没有什么建设性的结果。我一直在想其他的办法


Tags: dictrow1row2平铺办法口述建设性压平
2条回答

下面是一个工作实现,它首先检查哪些字典条目是列表,使用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'}]

您可以使用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'}]

相关问题 更多 >