擅长:python、mysql、java
<p>老实说,我没有得到你的代码,因为你有更多的循环和不必要的逻辑(<strong>我想</strong>)。所以我用我自己的方式来做。在</p>
<pre><code>import pprint
with open('corups.txt', 'r') as cr:
dic= {} # Empty dictionary
lines = cr.readlines()
for line in lines:
if line in dic: # If key already exists in dic then add 1 to its value
dic['line'] += 1
else:
dic['line'] = 1 # If key is not present in dic then create value as 1
pprint.pprint(dic) # Using pprint built in function to print dictionary data types
</code></pre>
<blockquote>
<p>If you are in real hurry then use <strong>collections.Counter</strong> </p>
</blockquote>