简体   繁体   English

TypeError:“元组”对象不支持字典中的项目分配

[英]TypeError: 'tuple' object does not support item assignment in dictionary

An increment could not be made on the corresponding values of the dictionary elements 无法对字典元素的相应值进行递增

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

Python tuples are immutable. Python tuples是不可变的。 Use list instead. 请改用list Like: 喜欢:

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

And then convert to tuples: 然后转换为元组:

sentiment_words = tuple(sentiment_words)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM