Skip to main content
 首页 » 编程设计

python之字典更新方法。 Python 3.4

2025年05月04日14youxin

想知道为什么该功能不起作用?

students = {'dsd': 13} 
 
student1 = {'dsdsd': 15} 
 
print(students.update(student1)) 

打印后它只会在控制台中显示 None。

请您参考如下方法:

因为 dict1.update(dict2) 用 dict2 的值更新 dict1 的值但不返回任何东西(因此打印 None 在你的情况下)。为了查看更新后的值,您需要执行以下操作:

students.update(student1) 
print(students) 

作为引用,检查dict.update() document其中说:

Update the dictionary with the key/value pairs from other, overwriting existing keys. Return None.