I have a list that is
我有一个列表
mylist = ['a', 'a', 'a', 'b', 'b', 'c', 'c', 'd']
And I used Counter from collections on this list to get the result:
我在此列表中使用了来自集合的Counter来获得结果:
from collection import Counter
counts = Counter(mylist)
#Counter({'a': 3, 'c': 2, 'b': 2, 'd': 1})
Now I want to subset this so that I have all elements that occur some number of times, for example: 2 times or more - so that the output looks like this:
现在我想要对其进行子集化,以便我拥有多次出现的所有元素,例如:2次或更多次 - 这样输出如下所示:
['a', 'b', 'c']
This seems like it should be a simple task - but I have not found anything that has helped me so far.
这似乎应该是一个简单的任务 - 但到目前为止我还没有找到任何帮助我的东西。
Can anyone suggest somewhere to look? I am also not attached to using Counter if I have taken the wrong approach. I should note I am new to python so I apologise if this is trivial.
任何人都可以建议去看看吗?如果我采取了错误的方法,我也不会使用Counter。我应该注意我是python的新手,所以如果这是微不足道的话我会道歉。
5 个解决方案
#1
5
[s for s, c in counts.iteritems() if c >= 2]
# => ['a', 'c', 'b']
#2
1
Try this...
尝试这个...
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)
Result:
结果:
[a, b, c]
#3
1
The usual way would be to use a list comprehension as @Adaman does.
In the special case of 2 or more, you can also subtract one Counter
from another
通常的方法是使用@Adaman的列表理解。在2或更多的特殊情况下,您也可以从另一个计数器中减去一个计数器
>>> counts = Counter(mylist) - Counter(set(mylist))
>>> counts.keys()
['a', 'c', 'b']
#4
0
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']
#5
0
I think above mentioned answers are better, but I believe this is the simplest method to understand:
我认为上面提到的答案更好,但我相信这是最简单的理解方法:
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']
#1
5
[s for s, c in counts.iteritems() if c >= 2]
# => ['a', 'c', 'b']
#2
1
Try this...
尝试这个...
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)
Result:
结果:
[a, b, c]
#3
1
The usual way would be to use a list comprehension as @Adaman does.
In the special case of 2 or more, you can also subtract one Counter
from another
通常的方法是使用@Adaman的列表理解。在2或更多的特殊情况下,您也可以从另一个计数器中减去一个计数器
>>> counts = Counter(mylist) - Counter(set(mylist))
>>> counts.keys()
['a', 'c', 'b']
#4
0
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']
#5
0
I think above mentioned answers are better, but I believe this is the simplest method to understand:
我认为上面提到的答案更好,但我相信这是最简单的理解方法:
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']