There are two dictionaries:
dict_1 = {'Петя Петров':0, 'Иван Иванов': 0, 'С.C. Сидоров': 0} dict_2 = {'Петров': 1, 'Иванов': 2, 'Шпак':3} It is necessary to write the values from dict_2 into the corresponding values of dict_1. If in the first dictionary there is no corresponding key, you need to inform about it. Upon learning that in the if / else construct, the words if and else do not have to be with equal indents, I tried to write the following code:
for k_2 in dict_2: for k_1 in dict_1: if k_2 in k_1: dict_1[k_1] += dict_2[k_2] break else: print(k_2, 'не найден') print(dict_1) Result:
Шпак не найден {'Петя Петров': 1, 'Иван Иванов': 2, 'С.C. Сидоров': 0} It seems to work. I want to know the opinion of professionals, does this code have a right to exist? What are other ways to solve my problem?
dict((k1,dict_2[k2]) if k2 in k1 else (k1, 'не найден') for k1, k2 in zip(dict_1, dict_2))- Igor Igoryanychdict_2can only occur surnames (a word in the second position)? - MaxU