Maicol Rojas
Maicol Rojas

Reputation: 93

compare a list in a dictionary python

I have to compare the keys of a dictionary with a list.

l = {"Carlos": 1131577, "Rodrigo": 3250239, "Marisol": 1174787}

with the words in

d = "Carlos Marisol"

For each word in the string that appears in the dictionary, I would like to add up the corresponding values.

The output should be something like this:

2306364
Carlos Marisol

The code I made is as follows:

l = {"Carlos": 1131577, "Rodrigo": 3250239, "Marisol": 1174787}
d = "Carlos Marisol"
a = d.split(' ')
n = 0
h = ""
f = len(a)
for k, v in l.items():
    if k == a[0] or k == a[1]:
        n += v
        h += k + " "
print(n)
print(h)

The output is as follows:

2306364
Carlos Marisol 

As you can see it works, but MANUALLY so to speak.

if k == a[0] or k == a[1]:

The variable a : a[0], a[1] must be iterable according to its positions, because adding another data does not work, how can I do it?

Because with this data it does not work correctly:

l = {"Rodrigo": 2155029, "Sebastian": 3290180, "Camilo": 4271313, "Marisol": 2964688}
d = "Marisol Sebastian Ana Camilo"

Upvotes: 1

Views: 103

Answers (5)

Mad Physicist
Mad Physicist

Reputation: 114578

You are using dictionaries backwards. Dictionaries are something to use for lookup, lists are to iterate. There are many ways of handling the case of missing keys.

In the examples below, I'm using the built-in sum function rather than writing a loop like

n = 0
for w in d.split():
    n += d.get(w, 0)

If all your names are in the dictionary, you can do direct lookup:

sum(l[w] for w in d.split())

If, on the other hand, your names aren't guaranteed to be in l you can use l.get:

sum(l.get(w, 0) for w in d.split())

This will return 10526181 for your second example.

Another way is to use collections.defaultdict for l, since the default int is 0:

l = collections.defaultdict(int, l)
sum(map(l.__getitem__, d.split()))

This sum formulation is equivalent to the first one, but does not crash on missing names because of the defaultdict.


To get a string with all the names in the dictionary, you can use a simple one-liner:

' '.join(w for w in d.split() if w in l)

Alternatively, you can use filter:

' '.join(filter(l.__contains__, d.split()))

Upvotes: 6

Henty
Henty

Reputation: 603

use comprehension, something like:

existing_words = [k for k, _ in l if k in d]
result_num = 0
result_str = ''
for word in existing_words:
    result_num += l[word]
    result_str += word + ' '

print(result_num)
print(d)

Upvotes: 1

Taron Qalashyan
Taron Qalashyan

Reputation: 723

Try this code:

l = {"Rodrigo": 2155029, "Sebastian": 3290180, "Camilo": 4271313, "Marisol": 2964688}
d = "Marisol Sebastian Ana Camilo"
a = d.split(' ')
n = 0
h = ""
for b in a:
    if (b in l): 
        n += l[b]
        h += b + " "
print(n)
print(h)

Upvotes: 1

LouisB
LouisB

Reputation: 1010

Assuming the first names are unique in l and in d

total = 0
first_names = d.split(" ")
for first_name in first_names:
    if first_name in list(l.keys()):
        total += l[first_name]

Upvotes: 1

Lei Yang
Lei Yang

Reputation: 4365

Just change if k == a[0] or k == a[1]: to

if k in a:

Upvotes: 2

Related Questions