我有几个单词的列表,需要每个单词的总数。你知道吗
两条线:
['ashtonsos', 'i', 'heard', 'you', 'shouldnt', 'trust', 'claires', 'with', 'piercings', 'lol']
['liveaidstyles', 'thank', 'you', 'so', 'much', '\xf0\x9f\x92\x98']
我已经导入了collections计数器,使用“from collections import counter”行
这是我的密码:
for word in words:
if word not in unique_words:
unique_words.append(word)
#print unique_words
tweet_count = Counter(unique_words)
for word in unique_words:
tweet_count.update()
for word in tweet_count:
print word, tweet_count[word]
打印的是每个单词后面跟一个1,即使这个单词是重复的。所以,基本上,计数器不计数。你知道吗
仅供参考,“.update()”行…我还使用了“tweet\u count+=1”。。。它返回相同的结果。你知道吗
我做错什么了??你知道吗
不是很明显吗?你在数一列
unique_words
。唯一的,根据定义,只发生一次。你知道吗试试这个:
修改为:
相关问题 更多 >
编程相关推荐