Python: understanding compiling two dictionaries

I am trying to write an understanding that will make up two dictionaries as follows:

d1 = {1:'a',2:'b',3:'c'} d2 = {'a':'A','b':'B','c':'C'} result = {1:'A',2:'B',3:'C'} 

That is, the resulting dictionary is formed from the keys of the first and the values ​​of the second for each pair, where the value of the first is equal to the key of the second.

This is what I still have:

 { k1:v2 for (k1,v1) in d1 for (k2,v2) in d2 if v1 == k2 } 

but that will not work. I'm new to Python, so I'm not sure if that really makes sense. By the way, I am using python 3.3.2.

Thanks in advance.

+7
python
source share
4 answers

One way to do this:

 result = {k: d2.get(v) for k, v in d1.items()} 

What behavior would you like to use for keys that don't matter in d2?

+9
source share

Just skip the d1 elements, and then for each element that you want to put in the result, do not use the value from d1 , but instead look at the new value in d2 , using the value d1 s as the key:

 >>> d1 = {1: 'a', 2: 'b', 3: 'c'} >>> d2 = {'a': 'A', 'b': 'B', 'c': 'C'} >>> d = {k: d2[v] for (k, v) in d1.items()} >>> d {1: 'A', 2: 'B', 3: 'C'} 
+6
source share

This method works even if not all values ​​in d1 are valid keys for d2 :

  {k1:d2[d1[k1]] for k1 in d1 if d1[k1] in d2} 
+2
source share
 This is what I've got so far: { k1:v2 for (k1,v1) in d1 for (k2,v2) in d2 if v1 == k2 } 

Two things you should note:

1) When you use the for-in loop directly on the dict:

 for (k1, v1) in some_dict: 

python loops over keys in a dict, i.e. this for-in loop is equivalent to:

 for (k1, v1) in some_dict.keys() 

But you tell python that each loop returns through the loop, (k1, v1), and python only returns every key through the loop. So this is a mistake. You can fix this by writing:

 for (k1, v1) in some_dict.items() 

The items () function returns two tuples: (key, value) every time through the loop.

2) Suppose you have this data:

 data = [ [1, 2, 3], ['a', 'b', 'c'] ] 

To iterate over each of the six values, it’s natural to write:

 results = [x for x in inner_array for inner_array in data] 

But this causes an error:

 Traceback (most recent call last): File "1.py", line 5, in <module> results = [x for x in inner_array for inner_array in data] NameError: name 'inner_array' is not defined 

To make it work, loops should be written backwards:

 results = [x for inner_array in data for x in inner_array] print results --output:-- [1, 2, 3, 'a', 'b', 'c'] 

I think the easiest way to remember this is: the loops are in the same order as if you wrote them without understanding the list:

 results = [] for inner_array in data: for x in inner_array: results.append(x) 

Personally, I would like to see that this has changed in python, so inside the list / dict / set definition you are working from the inside out, as you wrote it. In any case, this is what your code looks like with changes:

 d1 = {1:'a',2:'b',3:'c'} d2 = {'a':'A','b':'B','c':'C'} results = { k1: v2 for (k2,v2) in d2.items() for (k1,v1) in d1.items() if v1 == k2 } print results --output:-- {1: 'A', 2: 'B', 3: 'C'} 
+2
source share

All Articles