-2
I have the following dictionary and a list; from the dictionary I extracted the top 10 values. My question is this: I need to compare the keys of the respective values extracted from the dictMaior with the list. For example, Keys() do value() 72 is 19, and 19 is on the list? I need a code that returns me a list containing all the values of the dictMaior that is present in the list. I tried with items(), but it didn’t work ex. dictMaior = heapq.nlargest(10, dicionario.items())
import heapq
dicionario = {1: 68, 2: 53, 3: 57, 4: 56, 5: 60, 6: 58, 7: 62, 8: 53, 9: 57, 10: 64, 11: 53, 12: 54, 13: 64, 14: 58, 15: 63, 16: 61, 17: 56, 18: 64, 19: 72, 20: 65, 21: 63, 22: 55, 23: 57, 24: 62, 25: 65}
lista = [21, 20, 23, 8, 10, 18, 19, 2, 12, 14, 4, 9, 22, 16, 5]
# Extraindo os 10 maiores itens do dicionário através do values()
dictMaior = heapq.nlargest(10, dicionario.values())
# Saida do dictMaior:
# [72, 68, 65, 65, 64, 64, 64, 63, 63, 62]
+1, by the way: the function
most_common
can carry the number of desired pairsmost_common(10)
– JJoao