擅长:python、mysql、java
<p>首先,我将使用<code>with</code>来打开您的文件,而不仅仅是<code>open</code>。你知道吗</p>
<p>例如:</p>
<pre><code>with open(fname, "r+") as fhand:
</code></pre>
<p>而且,我认为你误解了字典的意义。它们是键/值存储,也就是说,每个键都是唯一的。你不能有多把钥匙。你知道吗</p>
<p>我认为更好的解决办法如下:</p>
<pre><code>import collections
for line in fhand:
line.rstrip()
if re.search(pattern , line):
x = re.findall(pattern , line)
lst.append(x)
else:
continue
counted = collections.Counter(lst)
print counted
</code></pre>
<p>这将返回一个包含列表中出现的键/值的字典</p>