开发者

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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜