二维字典新增数据

dic = dict()

def add_two_dimensional_dictionary(old_dict,key_a,key_b,value):
    if key_a in old_dict:
        old_dict[key_a].update({key_b:value})
    else:
        old_dict.update({key_a:{key_b:value}})

add_two_dimensional_dictionary(dic, 'a', 'a1', 11)
add_two_dimensional_dictionary(dic, 'b', 'b1', 21)
add_two_dimensional_dictionary(dic, 'b', 'b2', 22)

print(dic)

# {'a': {'a1': 11}, 'b': {'b1': 21, 'b2': 22}}

 

posted @ 2022-06-22 23:19  xuecl  阅读(44)  评论(0编辑  收藏  举报