Is there any other way to set a dict value using python?
This is my code:
a={}
a['b']=0
print a['b']
开发者_Python百科And this is my other code (that is wrong):
a={}
a.b=0
print a.b
Is there any other way to set the value associated with a key in a python dict
?
a = {'b':0}
or
a = dict([('b',0)])
or
a = dict.fromkeys(['b'], 0)
or
a = {}
b = {'b':0}
a.update(b)
or
a = {}
a.__setitem__('b',0)
精彩评论