Python:向字典dict() 添加新的键值对
向字典dict() 添加新的键值对 python添加键值对*(key-value)*用 dict.update()x = {1:2}print(x)#eg.1d = {3:4, 5:6, 7:8}x.update(d)print(x)#eg.2d2 = {1:2}x.update(d2)print(x)#eg.3d3 = {1:3}x.update(d3)print(x){1: 2}{1: 2,
·
向字典dict() 添加新的键值对 python
添加键值对*(key-value)*用 dict.update()
x = {1:2}
print(x)
#eg.1
d = {3:4, 5:6, 7:8}
x.update(d)
print(x)
#eg.2
d2 = {1:2}
x.update(d2)
print(x)
#eg.3
d3 = {1:3}
x.update(d3)
print(x)
{1: 2}
{1: 2, 3: 4, 5: 6, 7: 8}
{1: 2, 3: 4, 5: 6, 7: 8}
{1: 3, 3: 4, 5: 6, 7: 8}
需要注意的是,update() 只添加dict中没有的key-value。(见eg.2)
以key为准,如果已经有key则更新其value。(见eg.3)
更多推荐
已为社区贡献5条内容
所有评论(0)