簡體   English   中英

在用戶輸入其列表值的一個元素后,獲取字典的鍵

[英]get key of dictionary, after the user inputs one element of its list value

{'Juan Pérez': ['Programacion', 'Mecanica', 'Artes'], 'Mauricio Torres': ['Programacion', 'Calculo', 'Literatura']}

嘿。 我有一本字典,我想在用戶輸入其中一個選修類后收到該人的姓名:例如,用戶鍵入“Mecanica”,我收到“Juan Perez”

你可以嘗試這樣的事情:

some_dict = {'Juan Pérez': ['Programacion', 'Mecanica', 'Artes'], 'Mauricio Torres': ['Programacion', 'Calculo', 'Literatura']}

user_input = input("Type: ") # catches user's input

result = "" # variable for storing result
for key in some_dict: # iterating through all keys in dictionary
    if user_input in some_dict[key]: # checking if user's input is in value corresponding to key
        result = key
        
print(result)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM