Average, Minimum, and Maximum in a dictionary in python

0

I have a dictionary of this genre:

{walt: 0.942342, disney: 0.324234, robert: 0.562354, help: 0.546912, ...}

And I do this to find the mean and the maximum:

media = statistics.mean(dicContadores.values())
counter = collections.Counter(dicContadores.keys())
maximo = counter.most_common(1)
minimo = min(dicContadores.items(), key=lambda x: x[1])

print("   Média: ", media)
print("   Máximo: ", maximo)
print("   Minimo: ", minimo)

The way I have this output:

 Média: 0.0714285
 Máximo: [('walt', 1)]
 Minimo: ('disney', 0.324234)

But I have 1 problem: How do I make sure that the maximum associated value is not rounded?

    
asked by anonymous 18.12.2018 / 00:59

3 answers

4

Dictionaries defines an injection relationship between keys and values, a map, so it makes no sense to be orderly ( or rather, classifiable

To be able to rate the maximum and minimum, you need to create another structure that is classifiable. You already did this in the question when you used dicContadores.items() , which returns an iterable one containing tuples of two values being the key and the value, respectively.

But you can also do this implicitly through only the parameter key of max() and min() . When used, you implicitly create in memory another structure that will be used for sorting values.

maximo = max(dicContadores, key=dicContadores.get)
minimo = min(dicContadores, key=dicContadores.get)

Thus, maximo and minimo will be the keys where maximum and minimum values occur within the dictionary.

    
18.12.2018 / 11:38
0
d={"a": 3, "b": 2}
minimo = min(d, key=d.get)
print(minimo)
maximo = max(d, key=d.get)
print(maximo) código aqui

Now that's right, I'm really wrong.

    
19.12.2018 / 11:47
-2

You have the right medium? follow the maximum and minimum:

d={"walt": 0.342342, "disney": 0.324234}
minimo = d[min(d)]
print(minimo)
maximo = d[max(d)]
print(maximo)
    
18.12.2018 / 01:34