R
R
ratratrat2021-02-20 12:52:07
Python
ratratrat, 2021-02-20 12:52:07

How to get the value by key from the first dictionary and match it with the value by key from the second one?

We have a dictionary with subdictionaries, quite large:

{
    "Список": {
        "0": "*Аманжуров Тимур* —",
        "1": "*Блинов Денис* —",
        "2": "*Григорьева Мария* —",
        "3": "*Дедов Павел* —",
        "4": "*Дубков Александр* —",
        "5": "*Зелинский Даниил* —",
        "6": "*Зотов Денис* —",
        "7": "*Козлов Арсентий* —",
        "8": "*Колягин Артем* —",
        "9": "*Крутов Данил* —",
        "10": "*Кряженкова Алина* —",
        "11": "*Меньшов Геннадий* —",
        "12": "*Мишина Алина* —",
        "13": "*Наговский Даниил* —",
        "14": "*Нагорных Максим* —",
        "15": "*Овсянников Вячеслав* —",
        "16": "*Петренко Вадим* —",
        "17": "*Прощаев Максим* —",
        "18": "*Пугин Андрей* —", 
        "19": "*Сергеев Илья* —",
        "20": "*Совгирь Виктор* —",
        "21": "*Талызин Валерий* —",
        "22": "*Фокин Александр* —",
        "23": "*Хромов Андрей* —",
        "24": "*Чеснокова Анна* —",
        "25": "*Штернфельд Олег* —"
    },
    "Проценты": {
        "0": 21,
        "1": 12,
        "2": 21,
        "3": 22,
        "4": 15,
        "5": 23,
        "6": 21,
        "7": 19,
        "8": 21,
        "9": 25,
        "10": 35.4,
        "11": 15,
        "12": 26.4,
        "13": 23,
        "14": 22,
        "15": 26.4,
        "16": 0,
        "17": 17,
        "18": 22,
        "19": 0,
        "20": 44.1,
        "21": 10,
        "22": 10,
        "23": 26.8,
        "24": 30,
        "25": 25
    }
}

It is necessary to get the last name with the first name from the first dictionary "List" and compare it with the value from the second dictionary "Percentage".
For example, Amanzhurov has 21% - we substitute it for his last name
.
for x, y in zip(rating, percents):
    rating_cources = rating.get(str(key))
    percent_cources = percents.get(str(key))
    key += 1
    layout += f'{rating_cources} {percent_cources}%\n'

But how to make it so that if there was sorting (and it is, with an entry in a new dictionary), then it would be possible, having received the value by the key from the second dictionary ("Percentage"), to take the surname with the name and assign percentages to it, with this by changing the first dictionary "List", sorting it by percentage?
I think I explained more or less.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2021-02-20
@rattratrat

# проходим по словарю с фамилиями
for key, value in rating.items():
    rating_cources = value
    # и по ключу из первого словаря берем значения из второго
    percent_cources = percents.get(key)
    layout += f'{rating_cources} {percent_cources}%\n'

If you need sorting by percentages, then you can do the opposite - go through the second dictionary, sorted by percentages, and get the last names from the first one by key

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question