Как мне проанализировать словарное значение, объединяющее два слова в качестве ключа из списка слов?

Я хочу выполнить поиск в этом словаре, чтобы узнать, есть ли в нем совпадение ключей из элементов списка. Список "слов" - это вводимые пользователем данные, которые я разделил, поэтому я не могу изменить его на слова = ["третий dict"]

words = ["hello", "hi", "first", "morning", "dict", "third", "seconddict"]

groups = {
    "firstdict": {
        "one": "first dict #1",
        "two": "first dict #2",
        "three": "first dict #3",
        "four": "first dict #4"
    },
    "seconddict": {
        "one": "second dict #1",
        "two": "second dict #2",
        "three": "second dict #3",
        "four": "second dict #4",
        "five": "second dict #5",
        "six": "second dict #6"
    },
    "third dict":{
        "one": "third dict #1",
        "two": "third dict #2",
        "three": "third dict #3",
        "four": "third dict #4",
        "five": "third dict #5"
    }
}

Я могу использовать это, чтобы получить то, что находится внутри словаря, с помощью односторонних ключей

for word in words:
    if word in groups:
        print(groups[word])

Какие отпечатки

{'one': 'first dict #1', 'two': 'first dict #2', 'three': 'first dict #3', 'four': 'first dict #4'}
{'one': 'second dict #1', 'two': 'second dict #2', 'three': 'second dict #3', 'four': 'second dict #4', 'five': 'second dict #5', 'six': 'second dict #6'}

Как мне использовать список "слова" для поиска в словаре ключа "третий диктант" и заставить его распечатать это

{'one': 'third dict #1', 'two': 'third dict #2', 'three': 'third dict #3', 'four': 'third dict #4', 'five': 'third dict #5'}
0
Exodus 15 Окт 2020 в 11:47

1 ответ

Лучший ответ

Я думаю, это может вам помочь.

Измененный код

for word1 in words:
    for word2 in words:
        if word1 + ' ' + word2 in groups:
            print(groups[word1 + ' ' + word2])

Вывод

{'one': 'third dict #1', 'two': 'third dict #2', 'three': 'third dict #3', 'four': 'third dict #4', 'five': 'third dict #5'}
0
KittoMi 15 Окт 2020 в 10:46