我有一本带有 unicode 键的字典,我似乎无法操作里面的元素
state_sentiment = {u'WA': [0.0], u'DC': [-2.0, 0.0], u'WI': [0.0, 0.0, 0.0], u'WV': [0.0], u'FL': [2.0, 0.0, -2.0, 0.0, 0.0, 1.0], u'OR': [6.0]}
for k,v in state_sentiment:
max_score = -10.00
happiest_state = ''
current_score = float(sum(v))/len(v)
if current_score > max_score:
max_score = current_score
happiest_state = state_sentiment[k]
我得到错误
Traceback (most recent call last):
File "happiest_state.py", line 61, in <module>
processing()
File "happiest_state.py", line 55, in processing
readtweets(tweet_file, sent_dict)
File "happiest_state.py", line 38, in readtweets
current_score = float(sum(v))/len(v)
TypeError: unsupported operand type(s) for +: 'int' and 'unicode'
如果我从 切换v
到state_sentiment[k]
,仍然有错误
Traceback (most recent call last):
File "happiest_state.py", line 59, in <module>
processing()
File "happiest_state.py", line 53, in processing
readtweets(tweet_file, sent_dict)
File "happiest_state.py", line 36, in readtweets
current_score = float(sum(state_sentiment[k]))/len(state_sentiment[k])
KeyError: u'W'