擅长:python、mysql、java
<p>可以使用O(<em>n</em>log<em>n</em>)<a href="https://docs.python.org/3/library/itertools.html#itertools.groupby" rel="nofollow noreferrer">^{<cd1>}</a>(需要对输入列表进行预排序),但O(<em>n</em>)<a href="https://docs.python.org/3/library/collections.html#collections.Counter" rel="nofollow noreferrer">^{<cd2>}</a>就足够了:</p>
<pre><code>from collections import Counter
from operator import itemgetter
# construct dictionary mapping second value to count
c = Counter(map(itemgetter(1), entities))
# filter for second values with count > 1
dups = {value for value, count in c.items() if count > 1}
# filter entities with second value in dups
res = [entity for entity in entities if entity[1] in dups]
print(res)
# [('loc', 'blr'), ('cap', 'blr')]
</code></pre>