开发者

Is there any other way to set a dict value using python?

开发者 https://www.devze.com 2023-02-16 21:40 出处:网络
This is my code: a={} a[\'b\']=0 print a[\'b\'] 开发者_Python百科And this is my other code (that is wrong):

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

精彩评论

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

关注公众号