Python. How to modify/replace elements of a list which is a value in a dictionary?
Question:
I have two dictionaries:
D_1 = {1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E', 6: 'F', 7: 'G', 8: 'H', 9: 'I', 10: 'J', 11: 'K', 12: 'L', 13: 'M', 14: 'N', 15: 'O', 16: 'P', 17: 'Q', 18: 'R', 19: 'S', 20: 'T', 21: 'U', 22: 'V', 23: 'W', 24: 'X', 25: 'Y', 26: 'Z'}
D_2 = {1: ['A', 'A', 'R', 'O', 'N'], 2: ['A', 'B', 'B', 'E', 'Y'], 3: ['A', 'B', 'B', 'I', 'E']}
My task is to replace the letters in values of D2 with corresponding keys from D1. For example I would want the first key and value pair in D2 to look like this : {1:[1,1,19,16,15]}
I have modified D2 where i made the values into lists instead of strings thinking that it would make my task easier.
Answers:
dd = {v:k for (k, v) in D_1.items()}
{x: [dd[a] for a in y] for (x, y) in D_2.items()}
First line reverts D_1
. Second line applies dd
to values in D_2
.
Using ord
D_2 = {1: ['A', 'A', 'R', 'O', 'N'], 2: ['A', 'B', 'B', 'E', 'Y'], 3: ['A', 'B', 'B', 'I', 'E']}
{k: [ord(val)-64 for val in v] for (k, v) in D_2.items()}
Output:
{1: [1, 1, 18, 15, 14], 2: [1, 2, 2, 5, 25], 3: [1, 2, 2, 9, 5]}
I think your D_1
is redundant. What you can do here is to use ascii_uppercase
:
from string import ascii_uppercase
{k: [ascii_uppercase.index(i) + 1 for i in v] for k, v in D_2.items()}
Output:
{1: [1, 1, 18, 15, 14], 2: [1, 2, 2, 5, 25], 3: [1, 2, 2, 9, 5]}
m = map(lambda v:[{v: k for k, v in D_1.items()}[e] for e in v], D_2.values())
encapsulated in a map and create the new dictionary when useful:
dict(list(enumerate(m, start=1)))
# {1: [1, 1, 18, 15, 14], 2: [1, 2, 2, 5, 25], 3: [1, 2, 2, 9, 5]}
I have two dictionaries:
D_1 = {1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E', 6: 'F', 7: 'G', 8: 'H', 9: 'I', 10: 'J', 11: 'K', 12: 'L', 13: 'M', 14: 'N', 15: 'O', 16: 'P', 17: 'Q', 18: 'R', 19: 'S', 20: 'T', 21: 'U', 22: 'V', 23: 'W', 24: 'X', 25: 'Y', 26: 'Z'}
D_2 = {1: ['A', 'A', 'R', 'O', 'N'], 2: ['A', 'B', 'B', 'E', 'Y'], 3: ['A', 'B', 'B', 'I', 'E']}
My task is to replace the letters in values of D2 with corresponding keys from D1. For example I would want the first key and value pair in D2 to look like this : {1:[1,1,19,16,15]}
I have modified D2 where i made the values into lists instead of strings thinking that it would make my task easier.
dd = {v:k for (k, v) in D_1.items()}
{x: [dd[a] for a in y] for (x, y) in D_2.items()}
First line reverts D_1
. Second line applies dd
to values in D_2
.
Using ord
D_2 = {1: ['A', 'A', 'R', 'O', 'N'], 2: ['A', 'B', 'B', 'E', 'Y'], 3: ['A', 'B', 'B', 'I', 'E']}
{k: [ord(val)-64 for val in v] for (k, v) in D_2.items()}
Output:
{1: [1, 1, 18, 15, 14], 2: [1, 2, 2, 5, 25], 3: [1, 2, 2, 9, 5]}
I think your D_1
is redundant. What you can do here is to use ascii_uppercase
:
from string import ascii_uppercase
{k: [ascii_uppercase.index(i) + 1 for i in v] for k, v in D_2.items()}
Output:
{1: [1, 1, 18, 15, 14], 2: [1, 2, 2, 5, 25], 3: [1, 2, 2, 9, 5]}
m = map(lambda v:[{v: k for k, v in D_1.items()}[e] for e in v], D_2.values())
encapsulated in a map and create the new dictionary when useful:
dict(list(enumerate(m, start=1)))
# {1: [1, 1, 18, 15, 14], 2: [1, 2, 2, 5, 25], 3: [1, 2, 2, 9, 5]}