Skip to main content
 首页 » 编程设计

python之使用 update() 向字典添加值

2025年05月04日13JeffreyZhao

我正在尝试向我的字典中添加一个值。

我的词典:

a = {'a':[a,b,c],'b':[e,f,g]} 

当我使用命令 a.update({'c':[j,k,l]}) 并尝试打印时,我得到的值为 None

我想得到

a = {'a':[a,b,c],'b':[e,f,g], 'c':[j,k,l]} 

请您参考如下方法:

update更新字典 就位 并返回 None。

打印 a 而不是 udpate 返回的值。

文档:

D.update([E, ]**F) -> **None**.  Update D from dict/iterable E and F. 
If E present and has a .keys() method, does:     for k in E: D[k] = E[k] 
If E present and lacks .keys() method, does:     for (k, v) in E: D[k] = v 
In either case, this is followed by: for k in F: D[k] = F[k]