gpt4 book ai didi

python - 用函数中的另一个字典替换字典的内容 - Python

转载 作者:太空狗 更新时间:2023-10-29 22:04:52 26 4
gpt4 key购买 nike

我有一本字典,我想将它作为参数传递给一个函数。执行这个函数后,我想改变字典。

这是我的尝试:

def func(dict):
dict = {'a': 5}

所以我想要发生的事情:

dict = {'b': 3}
func(dict)
print(dict) # to be {'a': 5}, not {'b': 3}

有办法实现吗?非常感谢您! :)

最佳答案

def func(dct):
dct.clear()
dct.update({'a': 5})

关于python - 用函数中的另一个字典替换字典的内容 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23159019/

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