如果我有一个函数必须为字典内的嵌套字典执行。那我该如何执行它?Python字典遍历特定操作/函数的嵌套字典
例如:
# 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}
但是如果有很多的嵌套字典?我对此感到困惑?有什么建议么?
在此先感谢。
递归调用? – bharadhwaj
@bharadhwaj是 – Bhargav