更新多级字典中某个键的值;列表中的键使用python



如果python中的列表中有键,如何更新字典中键的值?

# data is dictionary containing multi-level key value
key_list = ['key1','key2','key3']
print(data[key1][key2][key3])
from flatten_dict import flatten
from flatten_dict import unflatten
# Flattern the data dict
flatten_data = flatten(data)
key_tuple = tuple(key_list)
flatten_data[key_tuple]= newValue # Add your new value here
#  Unflatten the data dict
data = unflatten(flatten_data)

相关内容

  • 没有找到相关文章

最新更新