對不起,我搜索了這個,它似乎很簡單,但我無法弄清楚。我想在我的字典值賦給一個列表:如何分配到字典中的列表
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行:類型錯誤:文件行10:「海峽」對象不支持項目分配#
爲什麼我不能爲第0個元素分配一個字符串?這是怎麼不一樣:
test = ["", "", ""]
test[0] = "test"
print test
回答: '測試', '', '']
你有沒有得到它的工作? – timgeb