簡體   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