简体   繁体   中英

how to find the most common elements in a list (using all())

I'm trying to return the most common elements in a list (statistical mode). Unfortunately I don't know how to use the all with an iterable. This is what it looks like if I don't use all():

def large(a):
for i in set(a):
    for j in set(a):
        if a.count(i)<a.count(j):
            break
return i

However I know that this can be written more eloquently. Can someone please write the more eloquent version as well? I believe it is something along the lines of:

[i for i,j in set(a) if all(a.count(i)>a.count(j)]

This code has 2 issues. First is that set(a) needs a second value to unpack and the second is the all doesn't work here.

Help me out please. Thanks!

Example: in {'a','a','b','b','b','c'} you would expect ' b ' to be the largest element

OK - understand your question now. The code below is not how one should generally solve this problem. But it's OK for learning how all() works. Please note that it's far less efficient than Counter. Interestingly it will return every element that is most frequent - so might be useful when accurate handling of multi-modal data is required.

>>> q = list("aaabbbbcc")
>>> q
['a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c']
>>> r = set(q)
>>> r
set(['a', 'c', 'b'])
>>> [x for x in r if all([q.count(x) >= q.count(y) for y in r])]
['b']
from collections import Counter

def most_common(a):
    return Counter(a).most_common(1)[0][0]

If you absolutely have to do this with all - which I don't think is the best approach - try

def most_common(a):
    a_count = [(i, a.count(i)) for i in set(a)]
    for i,c in a_count:
        if all(c >= cc for ii,cc in a_count):
           return i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM