Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对特定操作/函数的嵌套字典进行迭代的Python字典_Python_Dictionary - Fatal编程技术网

对特定操作/函数的嵌套字典进行迭代的Python字典

对特定操作/函数的嵌套字典进行迭代的Python字典,python,dictionary,Python,Dictionary,如果我有一个函数,它必须为字典中的嵌套字典执行。那我该怎么执行呢 例如: # I have the below dictionary d = {'a':1, 'b':2, 'c':3, 'd': {'e':4, 'f':5}} # I wanted a result as below {'a': 1, 'b': 2, 'c': 3, 'e': 4, 'f': 5} #I have executed it by for i, j in d.items(): if type(j)

如果我有一个函数,它必须为字典中的嵌套字典执行。那我该怎么执行呢

例如:

# I have the below dictionary
d = {'a':1, 'b':2, 'c':3, 'd': {'e':4, 'f':5}}

# I wanted a result as below
{'a': 1, 'b': 2, 'c': 3, 'e': 4, 'f': 5}

#I have executed it by 

for i, j in d.items():

    if type(j) == dict:
        for key,value in d[i].items():
            d[key] = value
        d.pop(i, None)

print d

#output
{'a': 1, 'c': 3, 'b': 2, 'e': 4, 'f': 5}
但是如果有很多嵌套字典呢?我对此有点困惑?有什么建议吗


提前感谢。

我建议这是一种扁平化:

def flatten(d):
    es = [d.pop(k) for k in sorted(d) if isinstance(d[k], dict)]
    # Due to sorted the dictionaries those keys that are lexicographically after 
    # will overwrite the key-value pairs from those that are before.

    # One would expect that `d.update(*es)` would work 
    # but it doesn't as `update` only takes one argument.
    for e in es:
        d.update(e)

def flatten_many(d):
    while any(isinstance(d[k], dict) for k in d):
        flatten(d)

第一个函数从
d
弹出每个字典,然后用它们更新
d
。当有一个值是字典时,第二个函数应用第一个函数

我建议这是一种扁平化:

def flatten(d):
    es = [d.pop(k) for k in sorted(d) if isinstance(d[k], dict)]
    # Due to sorted the dictionaries those keys that are lexicographically after 
    # will overwrite the key-value pairs from those that are before.

    # One would expect that `d.update(*es)` would work 
    # but it doesn't as `update` only takes one argument.
    for e in es:
        d.update(e)

def flatten_many(d):
    while any(isinstance(d[k], dict) for k in d):
        flatten(d)
dd={}
def myprint(d):
     for k, v in d.iteritems():
         if isinstance(v, dict):
             myprint(v)
         else:
             dd.update({k:v})
     return dd


d={'a':1, 'b':2, 'c':3, 'd': {'e':4, 'f':5,'g':{'h':6}}}
print(myprint(d))
第一个函数从
d
弹出每个字典,然后用它们更新
d
。当有一个值是字典时,第二个函数应用第一个函数

dd={}
def myprint(d):
     for k, v in d.iteritems():
         if isinstance(v, dict):
             myprint(v)
         else:
             dd.update({k:v})
     return dd


d={'a':1, 'b':2, 'c':3, 'd': {'e':4, 'f':5,'g':{'h':6}}}
print(myprint(d))
输出- {'a':1,'c':3,'b':2,'e':4,'f':5,'h':6}

输出-
{'a':1,'c':3,'b':2,'e':4,'f':5,'h':6}

递归调用?@bharadhwaj Yes递归调用?@bharadhwaj yesThanks。。它让我对嵌套字典的递归函数有了深刻的认识。我会多练习。:)Dan D.的答案中没有递归的(自调用)。一切都是迭代的,因为flatte()是在flatte_many()的while循环中调用的。@ChristianKönig但我认为pop函数是在这里递归执行的。如果我错了,请纠正我。重复不同于递归。有关详细说明,请参阅例如。谢谢。。它让我对嵌套字典的递归函数有了深刻的认识。我会多练习。:)Dan D.的答案中没有递归的(自调用)。一切都是迭代的,因为flatte()是在flatte_many()的while循环中调用的。@ChristianKönig但我认为pop函数是在这里递归执行的。如果我错了,请纠正我。重复不同于递归。有关详细说明,请参阅例如。