简体   繁体   English

Python 以列表为值的字典,使用原始键值的所有可能组合创建字典列表

[英]Python dict with lists as values, create list of dicts with all possible combinations of original keys-values

I have a dict where all values are lists:我有一个字典,其中所有值都是列表:

original_dict = {"key_00": [0, 1],
                    "key_01": [2, 3]
                    }

I want to get a list of dicts, where to each key is associated one of the original corresponding values, forming all the possible combinations...in other words, like this:我想获得一个字典列表,其中每个键与原始对应值之一相关联,形成所有可能的组合......换句话说,像这样:

[
{"key_00": 0, "key_01": 2},
{"key_00": 0, "key_01": 3},
{"key_00": 1, "key_01": 2},
{"key_00": 1, "key_01": 3}
]

I tried using something like this:我尝试使用这样的东西:

res = []
for combs in product(*original_dict.values()):
    # zip used to perform cross keys combinations.
    res.append([{ele: cnt} for ele, cnt in zip(original_dict, combs)])

but this way I end up having但这样我最终有

[{'key_00': 0}, {'key_01': 2}]
[{'key_00': 0}, {'key_01': 3}]
[{'key_00': 1}, {'key_01': 2}]
[{'key_00': 1}, {'key_01': 3}]    

You can use list comprehesntion .您可以使用list comprehesntion

from itertools import product

original_dict = {"key_00": [0, 1],"key_01": [2, 3]}

res = [{ele: cnt for ele, cnt in zip(original_dict, combs)} 
       for combs in product(*original_dict.values())]

print(res)

Your code can fix like below:您的代码可以修复如下:

res = []
for combs in product(*original_dict.values()):
    res.append({ele: cnt for ele, cnt in zip(original_dict, combs)})

[
    {'key_00': 0, 'key_01': 2}, 
    {'key_00': 0, 'key_01': 3}, 
    {'key_00': 1, 'key_01': 2}, 
    {'key_00': 1, 'key_01': 3}
]

You have to use itertools.product():您必须使用 itertools.product():

import itertools
original_dict = {"key_00": [0, 1],
                 "key_01": [2, 3]}

keys, values = zip(*original_dict.items())
aim = [dict(zip(keys, v)) for v in itertools.product(*values)]
print(aim)

>>> [{'key_00': 0, 'key_01': 2},
     {'key_00': 0, 'key_01': 3},
     {'key_00': 1, 'key_01': 2},
     {'key_00': 1, 'key_01': 3}]

Try this尝试这个

    res.append([{ele: cnt for ele, cnt in zip(original_dict, combs)}])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中获取所有dict键值? - How to get all dict keys-values in python? 如何通过使用公共密钥对值进行求和,从一系列dicts创建单个Python dict? - How to create single Python dict from a list of dicts by summing values with common keys? 如何将列表的字典转换为具有所有组合的字典列表? - How do you turn a dict of lists into a list of dicts with all combinations? 如何在字典列表和字典的嵌套字典中获取所有键和值? - how to get all keys&values in nested dict of list-of-dicts and dicts? 基于dicts内部值的所有可能的dicts组合 - all possible combinations of dicts based on values inside dicts 具有键和值列表的列到具有字典列表的列 - Columns with list of keys and values to columns with lists of dicts Python 查找字典中的所有值是否相等 - Python find if all values are equal in dict of dicts 如何从 Dicts 列表中创建 Dicts,其中键从列表中的 Dict Keys 中缩回,同时保持不同的值 - How to create a Dicts From a List of Dicts which the Keys are retracted from Dict Keys inside the list while keeping different values 连接多个字典以创建新列表,并将值作为原始字典的值列表 - Join multiple dicts to create new list with value as list of values of original dict Python 字典:为字典中的所有键创建一个列表,为字典中的所有值创建另一个列表 - Python Dictionary: Create a list for all keys in dict, another for all values in dict
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM