I have two dictionaries. One with full name:

a = { 'Иванов Иван Иванович': 12, 'Петров Петр Петрович': 34 } 

And another with initials:

 b = { 'Иванов И. И.': 12, 'Петров П. П.': 34 } 

Full names in dictionaries and their meanings are absolutely identical. There is a line with the name:

 c = 'Иванов Иван Иванович', 'Петров П. П.', 'Сидоров Сидр Сидрович' 

And there is a formula: ^ 3 ... here should be the number assigned for the full name ... ^ A ... here is the last name ... ^ B ... initials with dots, separated by spaces. This is how the formula looks like:

 ^312^AИванов^BИ. И. 

I need to get values ​​from the dictionary and insert it into the formula for each full name. If Sodorov is not in the dictionaries, he will be without numbers. Expected Result:

 ^312^AИванов^BИ. И. ^334^AПетров^BП. П. ^AСидоров^BС. С. 

Only such inconvenient function came to my mind:

 def get_code(): av = list(c) word = 'Иванов Иван Иванович' if word in av: return '^312^AИванов^BИ. И.' d = get_code() 

Can you please tell how to fully automate this process? Thanks in advance for your reply.

    1 answer 1

     a = { 'Иванов Иван Иванович': 12, 'Петров Петр Петрович': 34 } b = { 'Иванов И. И.': 12, 'Петров П. П.': 34 } c = 'Иванов Иван Иванович', 'Петров П. П.', 'Сидоров Сидр Сидрович' for i in c: full_name = i.split(' ', 1) if '.' in i else i.split(' ') if '.' in i and i in b.keys(): print(f'^3{b[i]}^A{full_name[0]}^B{full_name[1]}') elif i in a.keys(): print(f'^3{a[i]}^A{full_name[0]}^B{full_name[1][0]}. {full_name[2][0]}.') else: if '.' in i: print(f'^A{full_name[0]}^B{full_name[1]}') else: print(f'^A{full_name[0]}^B{full_name[1][0]}. {full_name[2][0]}.') 
    • thank you for your reply. It worked. Thank you very much!!! - Ireen1985