擅长:python、mysql、java
<p>主要的问题是,您要迭代文件中每个单词的行,而不是相反。因此,第一个单词将占用文件的所有行,并且每个后续单词将有0个匹配项。在</p>
<p>相反,你应该这样做:</p>
<pre><code>def hash_count(name, words):
dic = {word:0 for word in words}
with open(name) as f:
for line in f:
line_text = line.split('|||')[4]
for word in words:
# Check if word appears as a hashtag in line_text
# If so, increment the count for word
return dic
</code></pre>