无法对字典元素的相应值进行增量
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
答案 0 :(得分:6)
Python tuples
是不可变的。请改用list
。像:
sentiment_words[word]=[0,0,0]
然后转换为元组:
sentiment_words = tuple(sentiment_words)