有没有其他方法可以使用python设置dict值?

时间:2011-03-16 01:52:04

标签: python dictionary

这是我的代码:

a={}
a['b']=0
print a['b']

这是我的其他代码(错误):

a={}
a.b=0
print a.b

有没有其他方法可以设置与python dict中的键相关联的值?

1 个答案:

答案 0 :(得分:3)

a = {'b':0}

a = dict([('b',0)])

a = dict.fromkeys(['b'], 0)

a = {}
b = {'b':0}
a.update(b)

a = {}
a.__setitem__('b',0)