d = {1:'one',2:'two'}
print d
#d.update(3:'three')#这样是错误的
#print d
d.update({3:'three'})#这样是正确的
print d
>>>
{1: 'one', 2: 'two'}
{1: 'one', 2: 'two', 3: 'three'}
>>>
本文介绍了一种在Python中正确更新字典的方法。通过示例代码展示了使用`update`方法的正确姿势,并对比了错误的更新方式。
d = {1:'one',2:'two'}
print d
#d.update(3:'three')#这样是错误的
#print d
d.update({3:'three'})#这样是正确的
print d
>>>
{1: 'one', 2: 'two'}
{1: 'one', 2: 'two', 3: 'three'}
>>>
9287

被折叠的 条评论
为什么被折叠?