用函数中的另一个字典替换字典的内容 - Python [英] Replace the content of dictionary with another dictionary in a function - Python

查看:37
本文介绍了用函数中的另一个字典替换字典的内容 - Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a dictionary, and I want to pass it as argument to a function. After executing this function, I want the dictionary changed.

这是我的尝试:

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

所以我想要发生的事情:

So what I want to happen:

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

有没有办法做到这一点?非常感谢您提前!:)

Is there a way to achieve this? Thank you very much in advance! :)

推荐答案

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

这篇关于用函数中的另一个字典替换字典的内容 - Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆