分割python字典以产生所有值的组合 [英] Split python dictionary to result in all combinations of values

查看:127
本文介绍了分割python字典以产生所有值的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  my_dict = {'a':[1,2],'b':[3],'c':{'d':[4,5],'e ':[6,7]}} 

我需要导出所有的组合,如下所示。

  {'a':1,'b':3,'c':{'d' e':6}} 
{'a':1,'b':3,'c':{'d':4,'e':7}}
{'a' 1,'b':3,'c':{'d':5,'e':6}}
{'a':1,'b':3,'c' ':5,'e':7}}
{'a':2,'b':3,'c':{'d':4,'e':6}}

等等。这里可能有任何级别的嵌套

请让我知道如何实现这个

我尝试的东西被粘贴在下面,但绝对无法到达

  def gen_combinations(data):
my_list = []
if isinstance(data,dict):
for k,v in data.iteritems():
if isinstance(v,dict):
gen_combinations(v)
elif isinstance(v,list):
for i in range (len(v)):
temp_dict = data.copy()
temp_dict [k] = v [i]
打印temp_dict

my_dict = {'a ':[1,2],'b':[3],'c':{'d':[4,5],'e':[6,7]}}

gen_combinations(my_dict)

哪些导致了



'pre> {'a':1,'c':{'e':[6,7],'d':[4,5]},'b' [3]}
{'a':2,'c':{'e':[6,7],'d':[4,5]},'b':[3]}
{'e':6,'d':[4,5]}
{'e':7,'d':[4,5]}
{'e' 6,7],'d':4}
{'e':[6,7],'d':5}
{'a':[1,2] :{'e':[6,7],'d':[4,5]},'b':3}

$ b $来自itertools的b

解决方案

  


my_dict = {'a' [1,2],'b':[3],'c':{'d':[4,5],'e':[6,7]}}


def process(d):
to_product = []#[[('a',1),('a',2)],[('b',3) ]
for k,v in d.items():
if isinstance(v,list):
to_product.append([(k,i)for i in v])
elif isinstance(v,dict):
to_product.append([(k,i)for进程(v)])
else:
to_product.append([(k ,v)])
返回[dict(l)for l in product(* to_product)]

for i in process(my_dict):
print(i)

输出:

  {'a':1,'b':3,'c':{'e':6,'d':4}} 
{'a' ,'b':3,'c':{'e':6,'d':4}}
{'a':1,'b':3,'c' :6,'d':5}}
{'a':2,'b':3,'c':{'e':6,'d':5}}
{ 'a':1,'b':3,'c':{'e':7,'d':4}}
{'a':2,'b':3,'c' :{'e':7,'d':4}}
{'a':1,'b':3,'c':{'e':7,'d':5}}
{'a':2,'b':3,'c':{'e':7,'d':5}}

更新:



正常工作的代码其中

  from itertools import product 


my_dict = {'a':[1,2],'e' [7],'f':{'x':[{'a':[3,5]},{'a':[4]}]}}

def process ):
to_produ ct = []#[[('a',1),('a',2)],[('b',3),],...]
for k,v in d。 item():
如果isinstance(v,list)和所有(isinstance(i,dict)for i in v):
#具体情况下,当列表的进程不同...
c = product(* list(process(i)for i in v))
to_product.append([(k,list(l))for c in c])
elif isinstance(v,list )
to_product.append([(k,i)for i in v])
elif isinstance(v,dict):
to_product.append([(k,i)for i在过程(v)])
else:
to_product.append([(k,v)])
返回[dict(l)for l in product(* to_product)]

for i in process(my_dict):
print(i)

输出:

  {'f':{'x':[{'a':3},{'a' :4}]},'a':1,'e':7} 
{'f':{'x':[{'a':3},{'a':4}]} ,'a':2,'e':7}
{'f':{'x':[{'a ':5},{'a':4}]},'a':1,'e':7}
{'f':{'x':[{'a':5} {'a':4}]},'a':2,'e':7}


my_dict = {'a':[1,2], 'b':[3], 'c':{'d':[4,5], 'e':[6,7]}}

I need to derive all the combinations out of it as below.

{'a':1, 'b':3, 'c':{'d':4, 'e':6}}
{'a':1, 'b':3, 'c':{'d':4, 'e':7}}
{'a':1, 'b':3, 'c':{'d':5, 'e':6}}
{'a':1, 'b':3, 'c':{'d':5, 'e':7}}
{'a':2, 'b':3, 'c':{'d':4, 'e':6}}

and so on. There could be any level of nesting here
Please let me know how to achieve this
Something that I tried is pasted below but definitely was reaching nowhere

def gen_combinations(data):
    my_list =[]
    if isinstance(data, dict):
        for k, v in data.iteritems():
            if isinstance(v, dict):
                gen_combinations(v)
            elif isinstance(v, list):
                for i in range(len(v)):
                    temp_dict = data.copy()
                    temp_dict[k] = v[i]
                    print temp_dict

my_dict = {'a':[1,2], 'b':[3], 'c':{'d':[4,5], 'e':[6,7]}}

gen_combinations(my_dict)

Which resulted in

{'a': 1, 'c': {'e': [6, 7], 'd': [4, 5]}, 'b': [3]}
{'a': 2, 'c': {'e': [6, 7], 'd': [4, 5]}, 'b': [3]}
{'e': 6, 'd': [4, 5]}
{'e': 7, 'd': [4, 5]}
{'e': [6, 7], 'd': 4}
{'e': [6, 7], 'd': 5}
{'a': [1, 2], 'c': {'e': [6, 7], 'd': [4, 5]}, 'b': 3}

解决方案

from itertools import product


my_dict = {'a':[1,2], 'b':[3], 'c':{'d':[4,5], 'e':[6,7]}}


def process(d):
    to_product = []  # [[('a', 1), ('a', 2)], [('b', 3),], ...]
    for k, v in d.items():
        if isinstance(v, list):
            to_product.append([(k, i) for i in v])
        elif isinstance(v, dict):
            to_product.append([(k, i) for i in process(v)])
        else:
            to_product.append([(k, v)])
    return [dict(l) for l in product(*to_product)]

for i in process(my_dict):
    print(i)

Output:

{'a': 1, 'b': 3, 'c': {'e': 6, 'd': 4}}
{'a': 2, 'b': 3, 'c': {'e': 6, 'd': 4}}
{'a': 1, 'b': 3, 'c': {'e': 6, 'd': 5}}
{'a': 2, 'b': 3, 'c': {'e': 6, 'd': 5}}
{'a': 1, 'b': 3, 'c': {'e': 7, 'd': 4}}
{'a': 2, 'b': 3, 'c': {'e': 7, 'd': 4}}
{'a': 1, 'b': 3, 'c': {'e': 7, 'd': 5}}
{'a': 2, 'b': 3, 'c': {'e': 7, 'd': 5}}

Upd:

Code that works as asked here:

from itertools import product


my_dict = {'a':[1,2], 'e':[7], 'f':{'x':[{'a':[3,5]}, {'a':[4]}] } }

def process(d):
    to_product = []  # [[('a', 1), ('a', 2)], [('b', 3),], ...]
    for k, v in d.items():
        if isinstance(v, list) and all(isinstance(i, dict) for i in v):
            # specific case, when list of dicts process differently...
            c = product(*list(process(i) for i in v))
            to_product.append([(k, list(l)) for l in c])
        elif isinstance(v, list):
            to_product.append([(k, i) for i in v])
        elif isinstance(v, dict):
            to_product.append([(k, i) for i in process(v)])
        else:
            to_product.append([(k, v)])
    return [dict(l) for l in product(*to_product)]

for i in process(my_dict):
    print(i)

Output:

{'f': {'x': [{'a': 3}, {'a': 4}]}, 'a': 1, 'e': 7}
{'f': {'x': [{'a': 3}, {'a': 4}]}, 'a': 2, 'e': 7}
{'f': {'x': [{'a': 5}, {'a': 4}]}, 'a': 1, 'e': 7}
{'f': {'x': [{'a': 5}, {'a': 4}]}, 'a': 2, 'e': 7}

这篇关于分割python字典以产生所有值的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆