Get key by value
📁 dictionary, string, Advanced Python Topics
"
There is a dictionary:
d = {'a': '1', 'b': '2', 'c': '3'}and there is a string:
stroka = 'a3a2c'If an element of the string is among the values of the dictionary, it is necessary to output the key corresponding to that value. If it is among the keys, then output the value by key. It is necessary to output in two lines, values separately from keys.
I.e. for the string 'a3a2c' the expected output is '113' and 'cb'
I figured out how to output values by key:
"
Something like this:
d = { 1: '1', '2': 2, 3: '3',}def get_key(d, value): for k, v in d.items(): if v == value: return kprint(get_key(d, '1'))print(get_key(d, 2))print(get_key(d, 42))Console:
12 Getting a character by its Unicode number:
>>> chr(65)'A'>>> chr(122)'z'>>> print(chr(128522))?(in Python 2 it's better to use unichr instead of chr)
Getting the Unicode number by character:
>>> ord('A')65>>> ord Log in to leave an answer