为什么在更新字典时获得None?

Nik*_*mar 4 python dictionary

在此输入图像描述我基本上使用update方法合并两个词典.问题是我在合并python shell时工作但在执行时不在文件中.

v = {'customer_id': '9000', 'customer_name': 'Apple  Inc'}
b = {"a": "b"}

print v.update(b)
Run Code Online (Sandbox Code Playgroud)

上面的输出是 None

但它在shell中工作.我的愚蠢错误是什么?谢谢

Mat*_*711 7

v.update(b)正在更新b 到位.v确实更新了,但更新功能的结果None正是打印出来的.如果你做的事情

v.update(b)
print v
Run Code Online (Sandbox Code Playgroud)

你会看到v(更新)


Sai*_*sif 5

update函数返回None。所以

print v.update(b)  # this is printing out the return value of the update function.
Run Code Online (Sandbox Code Playgroud)

要打印出 dict 的更新值,只需再次打印 dict

print v  # This will print the updated value of v
Run Code Online (Sandbox Code Playgroud)