繁体   English   中英

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

[英]TypeError: 'tuple' object does not support item assignment in 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

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

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

然后转换为元组:

sentiment_words = tuple(sentiment_words)

暂无
暂无

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

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