擅长:python、mysql、java
<p>您可以很容易地使用<code>Counter</code>来实现这一点。计数<em>n</em>克的通用函数如下:</p>
<pre><code><b>from collections import Counter</b>
<b>from itertools import islice</b>
def count_ngrams(iterable,n=2):
return <b>Counter</b>(zip(*[<b>islice</b>(iterable,i,None) for i in range(n)]))</code></pre>
<p>这将产生:</p>
^{pr2}$