【发布时间】:2021-09-21 03:08:15
【问题描述】:
如果我想使用数组从字典中获取值,我会这样做:
def get_dict_with_arr(d, arr):
accumulator = d
for elem in arr:
accumulator = accumulator[elem]
return accumulator
并像这样使用它:
test_dict = {
'this': {
'is': {
'it': 'test'
}
}
}
get_dict_with_arr(test_dict, ['this', 'is', 'it']) # returns 'test'
我的问题是,我如何编写一个设置值而不是获取值的函数?基本上我想写一个set_dict_with_arr(d, arr, value)函数。
【问题讨论】:
标签: python arrays dictionary key