Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I return a new dictionary if the keys in one dictionary, match the keys in another dictionary?

Currently, I have a dictionary, with its key representing a zip code, and the values are also a dictionary.

d = { 94111: {'a': 5,  'b': 7,  'd': 7}, 
      95413: {'a': 6,  'd': 4}, 
      84131: {'a': 5,  'b': 15, 'c': 10, 'd': 11}, 
      73173: {'a': 15, 'c': 10, 'd': 15}, 
      80132: {'b': 7,  'c': 7,  'd': 7} }

And then a second dictionary, which associates which state the zip code belongs to.

states = {94111: "TX", 84131: "TX", 95413: "AL", 73173: "AL", 80132: "AL"}

If the zip code in the dictionary states matches one of the keys in db then it would sum up those values and put it into a new dictionary like the expected output.

Expected Output:

{'TX': {'a': 10, 'b': 22, 'd': 18, 'c': 10}, 'AL': {'a': 21, 'd': 26, 'c': 17, 'b': 7}}

So far this is the direction I am looking to go into but I'm not sure when both the keys match, how to create a dictionary that will look like the expected output.

def zips(d, states):
    result = dict()
    for key, value in db.items():
        for keys, values in states.items():
            if key == keys:


zips(d, states)
like image 262
inspire Avatar asked Jan 31 '19 11:01

inspire


People also ask

Can Python dictionary have identical keys?

The straight answer is NO. You can not have duplicate keys in a dictionary in Python.

Can a dictionary have two of the same keys?

No, each key in a dictionary should be unique. You can't have two keys with the same value. Attempting to use the same key again will just overwrite the previous value stored. If a key needs to store multiple values, then the value associated with the key should be a list or another dictionary.

Can a dictionary key be another dictionary?

Second, a dictionary key must be of a type that is immutable. For example, you can use an integer, float, string, or Boolean as a dictionary key. However, neither a list nor another dictionary can serve as a dictionary key, because lists and dictionaries are mutable.

What happens if you assign a new value to a dictionary using a key that already exists?

If the key is already present in the dictionary, it gets overwritten with the new value. The keys can also be passed as keyword arguments to this method with their corresponding values, like dictionary. update(new_key=new_value) .


1 Answers

Using collections module

Ex:

from collections import defaultdict, Counter

d = { 94111: {'a': 5,  'b': 7,  'd': 7}, 
      95413: {'a': 6,  'd': 4}, 
      84131: {'a': 5,  'b': 15, 'c': 10, 'd': 11}, 
      73173: {'a': 15, 'c': 10, 'd': 15}, 
      80132: {'b': 7,  'c': 7,  'd': 7} }

states = {94111: "TX", 84131: "TX", 95413: "AL", 73173: "AL", 80132: "AL"}

result = defaultdict(Counter)
for k,v in d.items():
    if k in states:
        result[states[k]] += Counter(v)
print(result)

Output:

defaultdict(<class 'collections.Counter'>, {'AL': Counter({'d': 26, 'a': 21, 'c': 17, 'b': 7}), 
'TX': Counter({'b': 22, 'd': 18, 'a': 10, 'c': 10})})
like image 114
Rakesh Avatar answered Sep 27 '22 22:09

Rakesh