擅长:python、mysql、java
<p>如果要删除至少两次以相同频率出现的所有名称:</p>
<pre><code>import collections
lst = ['Mike', 'Mike', 'Mike', 'Jhon', 'Jhon', 'Rob', 'Rob', 'Carl', 'Carl',
"foo","foo","foo","foo","bar", "bar", "bar", "bar"]
list_counter = collections.Counter(lst)
# count the frequency
times = collections.Counter(list_counter.values())
# keep names that do not appear at the same frequency as any other names
lst[:] = [name for name,v in list_counter.items() if times[v] < 2]
print(lst)
</code></pre>
<p>这会给你留下:</p>
<pre><code>['Mike']
</code></pre>
<p>如果还要确保计数不是1:</p>
<pre><code>[name for name, v in list_counter.items() if v == 1 or times[v] < 2]
</code></pre>
<p>如果要保留所有麦克风,请遍历列表:</p>
<pre><code>import collections
lst = ['Mike', 'Mike', 'Mike', 'Jhon', 'Jhon', 'Rob', 'Rob', 'Carl', 'Carl',
"foo","foo","foo","foo","bar", "bar", "bar", "bar"]
list_counter = collections.Counter(lst)
times = collections.Counter(list_counter.values())
lst[:] = [name for name in lst if times[list_counter[name]] < 2]
print(lst)
</code></pre>
<p>这会给你:</p>
<pre><code> ['Mike', 'Mike', 'Mike']
</code></pre>