我通过查看充满单词分数的字典来计算推文的情绪分数,并且对于每条推文中的每个单词,对每条推文的分数进行求和。每条推文都有一个与之关联的指定状态。因此,对于同一州内的所有推文,我想返回一个包含{'state',sum_of_all_sentiment_scores_per_state}的新词典。
我是否需要使用{'tweet',[]}创建一个新的dict hstates然后将textscore附加到每个state_key的列表中,然后使用Counter对每个状态列表中每个textcore的值求和?如何将值附加到列表中?
# Calculate final scores of all tweets based on tweet file order
hstates = {} #{'score', 'state'}
for item in tweet_place.items(): #tweet_place = {'tweet', 'place'}:
text = item[0]
state = item[1]
words = text.split()
textscore = 0
for word in words:
word = words.lower()
try:
textscore += scores[word] #sentiment scores dict
except:
pass #ignore tweet words not in sentiment scores dict
#now I have final score for each tweet
hstates[state].append(textscore)#breaks!!
print hstates.items()
答案 0 :(得分:1)
我会在这种情况下使用in
:
for word in words:
word = words.lower()
if word in scores:
textscore += scores[word] #sentiment scores dict
...
if state in hstates:
hstates[state].append(textscore)
else:
hstates[state] = [textscore]