其他分享
首页 > 其他分享> > 二维字典新增数据

二维字典新增数据

作者:互联网

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}}

 

标签:old,新增,two,dic,dimensional,二维,dict,key,字典
来源: https://www.cnblogs.com/xuecl/p/16403592.html