<p>一个简单的方法是使用<code>collections.Counter</code>:</p>
<pre><code>from collections import Counter
with open("file.txt","r") as f:
lineList = f.readlines()
for i,k in enumerate(map(lambda x:x.strip().split(" "),lineList)): # split each word
print(f"Line {i+1}:{Counter(k)}")
</code></pre>
<p>结果:</p>
<pre><code>Line 1:Counter({'urgently': 3, 'joyously': 1, 'truthfully': 1, 'seemingly': 1, 'broadly': 1, 'knowingly': 1, 'steadily': 1})
Line 2:Counter({'rigidly': 2, 'joyously': 1, 'urgently': 1, 'truthfully': 1, 'seemingly': 1, 'broadly': 1, 'suddenly': 1, 'healthily': 1, 'commonly': 1, 'often': 1})
Line 3:Counter({'totally': 2, 'tremendously': 1, 'steadily': 1, 'sharply': 1})
</code></pre>
<hr/>
<p>或者你可以用口述:</p>
<pre><code>l = ['joyously', 'urgently', 'truthfully', 'seemingly', 'broadly', 'knowingly', 'steadily', 'rigidly', 'suddenly', 'healthily', 'commonly', 'often', 'tremendously', 'totally', 'sharply']
with open("file.txt","r") as f:
lineList = f.readlines()
for line,i in enumerate(map(lambda x:x.strip().split(" "),lineList)):
d = dict.fromkeys(l, 0) # load a dict from the list
for j in i:
d[j] += 1
print(f"For line {line+1}:{d}")
</code></pre>
<p>结果:</p>
<pre><code>For line 1:{'joyously': 1, 'urgently': 3, 'truthfully': 1, 'seemingly': 1, 'broadly': 1, 'knowingly': 1, 'steadily': 1, 'rigidly': 0, 'suddenly': 0, 'healthily': 0, 'commonly': 0, 'often': 0, 'tremendously': 0, 'totally': 0, 'sharply': 0}
For line 2:{'joyously': 1, 'urgently': 1, 'truthfully': 1, 'seemingly': 1, 'broadly': 1, 'knowingly': 0, 'steadily': 0, 'rigidly': 2, 'suddenly': 1, 'healthily': 1, 'commonly': 1, 'often': 1, 'tremendously': 0, 'totally': 0, 'sharply': 0}
For line 3:{'joyously': 0, 'urgently': 0, 'truthfully': 0, 'seemingly': 0, 'broadly': 0, 'knowingly': 0, 'steadily': 1, 'rigidly': 0, 'suddenly': 0, 'healthily': 0, 'commonly': 0, 'often': 0, 'tremendously': 1, 'totally': 2, 'sharply': 1}
</code></pre>