字典更新方法。 Python 3.4

Dictionary update method. Python 3.4

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

students = {'dsd': 13}

student1 = {'dsdsd': 15}

print(students.update(student1))

打印后在控制台中显示 None

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

students.update(student1)
print(students)

作为参考,检查 dict.update() document 上面写着:

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

update 方法就地合并 dict 和 returns 'None',这就是您要打印的内容。您需要打印 students 本身。

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