擅长:python、mysql、java
<p>如果你只需要字母,你可以过滤非字母,你不需要在内存中存储任何额外的数据,你所要做的就是链接字符并每次跟踪前一个字符:</p>
<pre><code>from collections import Counter
from itertools import chain
with open("in.txt") as f:
prev = f.read(1)
c = Counter()
for ch in filter(str.isalpha, chain.from_iterable(f)):
c[prev + ch] += 1
prev = ch
print(c)
</code></pre>
<p>如果您想要所有字符,只需删除过滤器:</p>
^{pr2}$