【发布时间】:2016-02-19 17:24:50
【问题描述】:
对不起,我已经搜索过这个,它看起来很简单,但我无法弄清楚。我正在尝试将值分配给我的字典中的列表:
class Test(object):
def __init__(self):
self.test = { "color": ["", "", "", ""],
"incandescence": ["", "", "", ""] }
def setTest(self):
key = "color"
print "KEY is", key
self.test[key][0] = "TEST"
print self.test
def clearDict(self):
for key in self.test:
self.test[key] = ""
x = Test()
x.clearDict()
x.setTest()
错误:第 1 行:TypeError:文件第 10 行:“str”对象不支持项目分配#
为什么我不能将字符串分配给第 0 个元素?这怎么不一样:
test = ["", "", ""]
test[0] = "test"
print test
答案:['test', '', '']
【问题讨论】:
-
你让它工作了吗?
标签: python list dictionary assign