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.