Comparing dictionary values() with list values

Asked

Viewed 87 times

-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]

4 answers

1

Grateful to all who collaborated with me, but after eating a lot of documentation I arrived at the following code below that satisfied my need. I thought it was better to use "Collections" and it worked really well... Note: The list and the dictionary is just an adaptation of a csv of 1831 lines, just simplified to be able to post here... Thank you for all your efforts!

from collections import Counter

lista = [21, 20, 23, 8, 10, 18, 19, 2, 12, 14, 4, 9, 22, 16, 5]
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}
numeros_presentes = []

cnt = Counter(dicionario)
ordenado = cnt.most_common()
ordenado = ordenado[0:10]

for num in ordenado:
    if num[0] in lista:
        numeros_presentes.append(num[0])

print(numeros_presentes)

# Saída do código:
# [19, 20, 10, 18, 21]
  • 1

    +1, by the way: the function most_common can carry the number of desired pairsmost_common(10)

0

Well got a little confused even more because the return I got is an empty list but come on:

You have a list called: lista = [21, 20, 23, 8, 10, 18, 19, 2, 12, 14, 4, 9, 22, 16, 5]

and another list called: dictMaior = [72, 68, 65, 65, 64, 64, 64, 63, 63, 62]

What you need to check is if some dictMaior number is in the list then: numeros_presentes = [num for num in dictMaior if num in lista]

In other words num will be the name assigned to each value in the dictMaior then if num is present in lista it will be returned within numeros_presentes.

  • Filipe Gonçalves, grateful brother, was worth msm! You almost got there. But the big point here was to compare the keys of the dictionary referring to the values of the dictMaior with the list; for example: the key of 72 is 19, the key of 68 is 1, and the "19 and 1" are in the list? What I needed was this return, a list containing the numbers that are present in both places.

0

Only one variant of the @Claudinei version using lists in comprehension

from collections import Counter
lista = [ 21 ......
dic = { 1:68 ......
print([k for k,c in Counter(dic).most_common(10) if k in lista])

-1

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]
lista2 = []
a = 0
for x in lista:
   lista2.append(dicionario[x])
   a += 1
lista2.sort(reverse=True)
print(lista2)
  • 2

    Allan, you just pasted a code (no formatting).. Ideally you would explain what and how you did to get the result that AP needs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.