本文介绍了如何拆分python字典以匹配一个键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  my_dict1 = {'a':1,'chk':{'b':2,'c':3},'e':{'chk' f':5,'g':6}}} 

我想循环通过dict递归地,如果密钥为'chk',则将其拆分。

预期输出:

  {'a':1,'b':2,'e':{'f':5}} 
{'a':1,'c':3,'e' :5}}
{'a':1,'b':2,'e':{'g':6}}
{'a':1,'c' 'e':{'g':6}}

不知道如何实现这一点。请帮忙。

我试过的是下面的。

  temp_list = [] 
for k,v in my_dict1.iteritems():
temp = {}
如果k是chk:
为key,val在v.iteritems()中:
temp [key] = val
my_dict1 [k] = {}
for ky,vl在temp.iteritems()中:
my_new_dict = copy.deepcopy(my_dict1)
k,v in my_new_dict.iteritems():
如果k是chk:

my_new_dict [k] = {ky:vl}
temp_list.append(my_new_dict)
print temp_list

输出:



'pre> [{'a':1,'chk':('c',3),'e':{'chk':{'f' g':6}}},
{'a':1,'chk':('b',2),'e':{'chk':{'f':5,'g' :6}}}]

如何使其递归?

解决方案
  from itertools import product 


my_dict = {'a':1,'chk':{'b':2,'c':3},'e':{'chk':{'f' ,'g':6}}}


def process(d):
to_product = []#[[('a',1)],[(' b',2),('c',3)],...]
for k,v in d.items():
if k =='chk':
to_product.append([(k2,v2)
for d2 in process(v)
for k2,v2 in d2.items()])
elif isinstance(v,dict):
to_product.append(进程(v)中的d2的[(k,d2)])
else:
to_product.append([(k,v)])
lst = [dict(l)for l in product(* to_product)]
unique = []
[unique.append(item)for item in lst if item not in unique]
return unique

我正在处理(my_dict):
print(i)

#{'e':{'f':5},'b':2 ,'a':1}
#{'e':{'g':6},'b':2,'a':1}
#{'e':{'f ':5},' a':1,'c':3}
#{'e':{'g':6},'a':1,'c':3}


my_dict1 = {'a':1, 'chk':{'b':2, 'c':3}, 'e':{'chk':{'f':5, 'g':6}} }

I would like to loop through the dict recursively and if the key is 'chk', split it.
Expected output:

{'a':1, 'b':2, 'e':{'f':5}}
{'a':1, 'c':3, 'e':{'f':5}}
{'a':1, 'b':2, 'e':{'g':6}}
{'a':1, 'c':3, 'e':{'g':6}}

Not sure of how to achieve this. Please help.
What I have tried is below.

temp_list =[]
for k,v in my_dict1.iteritems():
    temp ={}
    if k is "chk":
        for key,val in v.iteritems():
            temp[key] = val
        my_dict1[k]={}
        for ky,vl in temp.iteritems():
            my_new_dict = copy.deepcopy(my_dict1)
            for k,v in my_new_dict.iteritems():
                if k is "chk":

                    my_new_dict[k] = {ky:vl}
            temp_list.append(my_new_dict)
print temp_list

output:

[{'a': 1, 'chk': ('c', 3), 'e': {'chk': {'f': 5, 'g': 6}}},
 {'a': 1, 'chk': ('b', 2), 'e': {'chk': {'f': 5, 'g': 6}}}]

How to make it recursive?

解决方案
from itertools import product


my_dict = {'a':1, 'chk':{'b':2, 'c':3}, 'e':{'chk':{'f':5, 'g':6}} }


def process(d):
    to_product = []  # [[('a', 1)], [('b', 2), ('c', 3)], ...]
    for k, v in d.items():
        if k == 'chk':
            to_product.append([(k2, v2) 
                              for d2 in process(v) 
                              for k2, v2 in d2.items()])
        elif isinstance(v, dict):
            to_product.append([(k, d2) for d2 in process(v)])
        else:
            to_product.append([(k, v)])
    lst = [dict(l) for l in product(*to_product)]
    unique = []
    [unique.append(item) for item in lst if item not in unique]
    return unique

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

# {'e': {'f': 5}, 'b': 2, 'a': 1}
# {'e': {'g': 6}, 'b': 2, 'a': 1}
# {'e': {'f': 5}, 'a': 1, 'c': 3}
# {'e': {'g': 6}, 'a': 1, 'c': 3}

这篇关于如何拆分python字典以匹配一个键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 00:39