【问题标题】:Is there any other way to set a dict value using python?有没有其他方法可以使用 python 设置 dict 值?
【发布时间】:2011-03-16 01:52:04
【问题描述】:

这是我的代码:

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

这是我的另一个代码(这是错误的):

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

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

【问题讨论】:

标签: python dictionary


【解决方案1】:
a = {'b':0}

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2019-11-06
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    相关资源
    最近更新 更多