简体   繁体   中英

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. Use list instead. Like:

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

And then convert to tuples:

sentiment_words = tuple(sentiment_words)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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