擅长:python、mysql、java
<p>试试这段代码</p>
<pre><code>word_vec = {}
# col 1 is the word, col 2 is the score, col 3 is the number of times it occurs
for i in range(len(data)):
sentence = data['SurveyResponse'][i].split(' ')
for word in sentence:
word_vec['word'] = word
if word in word_vec:
word_vec[word]['Score'] += data['SCORE'][i] # Keep accumulating the total score for each word, would be easier to find the average score later on
word_vec[word]['NumberOfTimes'] += 1
else:
word_vec[word] = {'Score':data['SCORE'][i], 'NumberOfTimes':1}
</code></pre>
<p>要增加'NumberOfTimes'的值,您可以像这样直接递增<code>word_vec[word]['NumberOfTimes'] += 1</code></p>