Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return elements with the highest occurrence in list?

So I was attempting to make a function which will takes a single argument numlist (a non-empty list of numbers), and returns the sorted list of numbers which appear with the highest frequency in numlist.

So far I have managed to create a dictionary (numbers as keys and their frequencies as values). But I still want to find which one of them has the maximum value and return it. Thing is I dont know how to compare them. Now there are certain one-liners like this

max(stats, key=stats.get)

But what if there are multiple values meeting that requirement?

mode([5, 1, 1, 5])
#I'm guessing this should return [1, 5] if sorted...

How would you handle it? Thanks for any help on that!

Here is my code:

def mode(numlist):

    mylist = numlist
    dic = {}

    for num in mylist:
        if num in dic:
            dic[num] += 1
        else:
            dic[num] = 1
     # try to get the max value and return them in a set form like [1, 0]    
like image 557
2Xchampion Avatar asked Apr 09 '16 11:04

2Xchampion


People also ask

What function returns the highest value in a list?

The MAX function in Excel returns the highest value in a set of data that you specify.

How do you find the maximum number of occurrences in Python?

Given a list, the task is to find the number of occurrences of the largest element of the list. Method 1: The naive approach is to find the largest element present in the list using max(list) function, then iterating through the list using a for loop and find the frequency of the largest element in the list.

How do you find the maximum number of repeated elements in a list in Python?

Method #1 : Using loop + count() In this, we perform iteration of elements and check if count is more than N of that element using count(), if yes, then we remove that element.


2 Answers

You can use a Counter which returns a dict like object where elements are stored as dictionary keys and their counts are stored as dictionary values.

from collections import Counter

def mode(my_list):
    ct = Counter(my_list)
    max_value = max(ct.values())
    return sorted(key for key, value in ct.items() if value == max_value)

Demo:

In [46]: mode([5, 1, 1, 5])
Out[46]: [1, 5]
like image 96
styvane Avatar answered Nov 15 '22 05:11

styvane


Sticking to your current implementation, you can sort the values of your dictionary, then just get all the keys of your dictionary that match that maximum value:

So, you can do something like this at the end of your method:

vals = max(dic.values())
return [k for k, v in dic.items() if v == vals]

vals will hold the maximum value to indicate that is the value of the repeating item. Then we create a list comprehension that goes through the dictionary to get all keys that match that value.

Putting it together:

def mode(numlist):

    mylist = numlist
    dic = {}

    for num in mylist:
        if num in dic:
            dic[num] += 1
        else:
            dic[num] = 1

    vals = max(dic.values())
    return [k for k, v in dic.items() if v == vals]


print(mode([5, 1, 1, 5]))

Output:

[1, 5]
like image 43
idjaw Avatar answered Nov 15 '22 04:11

idjaw