TypeError:'tuple'对象不支持字典中的项目赋值

时间:2013-12-18 06:41:29

标签: python dictionary

无法对字典元素的相应值进行增量

sentiment_words = {}
for word in TotalVector:
    if not word in sentiment_words:
        sentiment_words[word]=(0,0,0)
        #sentiment_word(positive,negative,neutral)
    if ispositive(word):
        sentiment_words[word][0] += 1
    elif isnegative(word):
        sentiment_words[word][1] += 1
    elif isneutral(word):
        sentiment_words[word][2] += 1

print sentiment_words

1 个答案:

答案 0 :(得分:6)

Python tuples是不可变的。请改用list。像:

sentiment_words[word]=[0,0,0]

然后转换为元组:

sentiment_words = tuple(sentiment_words)