擅长:python、mysql、java
<p>计数器使用<code>&</code>运算符支持多集交集:</p>
<pre><code>>>> from collections import Counter
>>> list1 = list("abba")
>>> list2 = list("bbanana")
>>> c1 = Counter(list1)
>>> c2 = Counter(list2)
>>> sum(c1[k]*c2[k] for k in c1 & c2) # O(n)
10
>>> sum([x==y for x in list1 for y in list2]) # O(n**2)
10
</code></pre>