保留出现 N 次或更多次的字符串

Keep strings that occur N times or more

我有一个列表

mylist = ['a', 'a', 'a', 'b', 'b', 'c', 'c', 'd']

并且我使用了此列表中集合中的计数器来获得结果:

from collection import Counter
counts = Counter(mylist)

#Counter({'a': 3, 'c': 2, 'b': 2, 'd': 1})

现在我想对其进行子集化,以便我拥有出现一定次数的所有元素,例如:2 次或更多次 - 因此输出如下所示:

['a', 'b', 'c']

这似乎应该是一项简单的任务 - 但到目前为止我还没有找到任何对我有帮助的东西。

谁能推荐个地方看看?如果我采取了错误的方法,我也不喜欢使用 Counter。我应该注意到我是 python 的新手,所以如果这是微不足道的,我深表歉意。

试试这个...

def get_duplicatesarrval(arrval):
    dup_array = arrval[:]
    for i in set(arrval):
        dup_array.remove(i)       
    return list(set(dup_array))   



mylist = ['a', 'a', 'a', 'b', 'b', 'c', 'c', 'd']
print get_duplicatesarrval(mylist)

结果:

[a, b, c]
[s for s, c in counts.iteritems() if c >= 2]
# => ['a', 'c', 'b']

通常的方法是像@Adaman 那样使用列表理解。
在 2 或更多的特殊情况下,您还可以从另一个

中减去一个 Counter
>>> counts = Counter(mylist) - Counter(set(mylist))
>>> counts.keys()
['a', 'c', 'b']
from itertools import groupby

mylist = ['a', 'a', 'a', 'b', 'b', 'c', 'c', 'd']

res = [i for i,j in groupby(mylist) if len(list(j))>=2]

print res
['a', 'b', 'c']

我觉得上面提到的答案更好,但我相信这是最简单的理解方法:

mylist = ['a', 'a', 'a', 'b', 'b', 'c', 'c', 'd']
newlist=[]
newlist.append(mylist[0])
for i in mylist:
    if i in newlist:
        continue
    else:
        newlist.append(i)
print newlist

>>>['a', 'b', 'c', 'd']