Maximum occurrences in a python dictionary

3

If I have the dictionary:

meu_dic = {A:3, B:5, C:0, D:10, E:2}

resulting from:

meu_dic = {i:lista.count(i) for i in lista}

I know A appears 3 times in the list, B 5 times, etc. How can I return the maximum number of reps and their key? That is, for this dictionary would have to return: 10, D .

    
asked by anonymous 27.05.2016 / 16:47

3 answers

1
>>> max(map(tuple, map(reversed, Meu_dic.items())))
(10, 'D')

EDIT:

I thought of another way:

>>> max([i[::-1] for i in Meu_dic.items()])
(10, 'D')
    
27.05.2016 / 18:35
1

Make:

meu_dict = {'A':3, 'B':5, 'C':0, 'D':10, 'E':2}
max_value = sorted(meu_dict.items(), key=lambda tup: tup[1])[-1] # ('D', 10)
    
27.05.2016 / 17:03
0

As simple as possible:

key = max(dic, key=dic.get)
value = dic[key]
print key, value

dic is your dictionary.

    
01.06.2016 / 20:14