如何检查一个单词是否在作为字典键的列表中?

How check if a word is in a list that is a key of a dictionary?

我有这样的字典:

dictionary={('I', 'We', 'They'): 'X'}

例如,我想检查这本词典中是否有 'I',如果是真的,return 'X'。有什么解决办法吗?

我想您想要在字典中添加更多元素,因此您将不得不遍历字典的键,并且 return 当您找到正确的键时。像这样:

def find_element(element):
    keys = dictionary.keys()
    for key in keys:
        if element in key:
            return dictionary[key]

您可以使用“I”作为元素来调用此函数,您会为您的键找到正确的值。