问题描述
目前我已经对字典进行了 bfs 遍历,如下所示:
def bfs_traversal(dictionary):
dictionary_array = [dictionary]
for sub_dictionary in dictionary_array:
if type(sub_dictionary) is dict:
for key,value in sub_dictionary.items():
print("key=",key)
print("value",value)
if type(value) is dict:
dictionary_array.append(value)
我想在 bfs_traversal 函数中更改字典副本的值,这是一个概念:
def bfs_traversal(dictionary):
dictionary_array = [dictionary]
dictionary_copy = dictionary.copy()
for sub_dictionary in dictionary_array:
if type(sub_dictionary) is dict:
for key,value in sub_dictionary.items():
#Somehow access dictionary_copy by using keys - in order to change value at this given key
#Like: dictionary_copy['1-1']['C-2']['I-3']
print("key=",value)
if type(value) is dict:
dictionary_array.append(value)
return dictionary_copy
所以结果是这样的:
原文:
{'1-1': {'A-2': "first inside 1-1",'B-2': "second inside 1-1",'C-2': {'I-3': "first inside C-2",'II-3': 'second inside C-2'}},'2-1': "second"}
从 bfs_traversal 返回的副本:
{'1-1': {'A-2': "first inside 1-1 but changed",'C-2': {'I-3': "first inside C-2 but changed",'2-1': "second"}
你知道我该怎么做吗? 理想的情况是访问字典,如:
dictionary_copy['1-1']['C-2']['I-3']
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)