gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:09:16 24 4
gpt4 key购买 nike

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

我的词典:

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]

关于python - 使用 update() 向字典添加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20358465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com