简体   繁体   English

检查用户输入和具有多个值的字典键之间的匹配

[英]Checking for match between user input and dictionary key with multiple values

If I want to check if the inputted parameter in the checkPlayer function matches a key in dict1 and return the entire object or a specific value for the respective key, how would I change this code? 如果我要检查,如果在输入参数checkPlayer功能相匹配的关键dict1并返回整个对象或特定值相应的键,我将如何改变这种代码?

dict1 = {'Messi' : ('Argentina','Barcelona'), 'Ronaldo' : ('Portugal','Juventus'), 'Robben': ('Netherlands','Bayern')}

def checkPlayer(plyr):
    for x in dict1:
        if plyr == x:
           print(x)

checkPlayer('Messi')

I am mainly confused as to why printing x would print the players name and not the whole object. 我很困惑为什么打印x会打印玩家名字而不是整个对象。 How would I print the whole object , or more specifically, just the nationality or team if a match was found. 我将如何打印整个对象 ,或者更具体地说, 只是国籍或团队,如果找到匹配。 Not quite sure how to access indexes in dictionaries using for loops, it's not quite as intuitive as lists :/ 不太确定如何使用for循环访问字典中的索引,它不像列表那样直观:/

Thanks guys 多谢你们

You are iterating over the keys of the dictionary and printing the key (which is the name of the player) if it was found. 您正在迭代字典的键并打印密钥 (这是播放器的名称),如果找到它。 To get the value for that key, you can use the get method (which also allows you to specify a default if a key wasn't found in the dictionary): 要获取该键的值,可以使用get方法(如果在字典中找不到键,还可以指定默认值):

dict1 = {'Messi' : ('Argentina','Barcelona'), 'Ronaldo' : ('Portugal','Juventus'), 'Robben': ('Netherlands','Bayern')}

def checkPlayer(plyr):
    print(dict1.get(plyr))

checkPlayer('Messi')

Output 产量

('Argentina', 'Barcelona')

Furthermore, to check if a player name is in your dictionary, you can simply have if plyr in dict1 ; 此外,要检查一个玩家名字是否在你的字典中,你可以简单地if plyr in dict1 ; you don't need to iterate over all the keys. 你不需要迭代所有键。 So your method can also be: 所以你的方法也可以是:

def checkPlayer(plyr):
    if plyr in dict1:
        print(plyr, dict1[plyr][1])

checkPlayer('Messi')

Output 产量

Messi Barcelona

your object is a key:value the key is 'Messi' the value is 'Argentina','Barcelona' however in your print statement you only print the name of object will will return the key only without the value 你的对象是一个关键:值键是'Messi',值是'Argentina','Barcelona'但是在你的print语句中你只打印对象的名称将只返回没有值的键
to access the value you may use get() or the index of the object inside your dictionary 访问您可能使用的值get()或字典中对象的索引

to get your desire result use this : 得到你想要的结果使用这个:

print plyr,':',dict1[plyr]

You can use below code: 您可以使用以下代码:

dict1 = {'Messi' : ('Argentina','Barcelona'), 'Ronaldo' : ('Portugal','Juventus'), 'Robben': ('Netherlands','Bayern')}

checkPlayer = 'Messi'
print (dict1.get(checkPlayer,'Player not found')) # 'Player not found' will be returned if checkPLayer is not in the dict1.keys()
#result --> ('Argentina', 'Barcelona')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用用户输入从字典中的一个键打印多个值 - printing multiple values from a key in a dictionary with user input 如何创建一个字典,允许用户输入中的多个键并打印出组合在一个字符串中的键值? - How to create a dictionary that allows multiple keys in a user input and print out key values combined in one string? 如果在用户输入中找到键,则从字典对象打印多个键和值 - Printing multiple keys and values from a dictionary object if key is found in a user input 根据句子中的字典值检查字典键 - Checking for dictionary key based on dictionary values in a sentence Dictionary.get每个键具有多个值-没有匹配项 - Dictionary.get with multiple values per key - no match 有没有更好的方法来检查字典中是否存在多个键并根据找到的键为变量分配多个值 - is there a better way of checking if multiple key exists in a dictionary and assigning multiple values to a variable based on the key found 字典中某个键的多个值 - Multiple values for a key in dictionary Python:如何使用字典调用方法(字典中的值)以根据用户输入(字典中的键)在不同的函数中运行? - Python: How to use a dictionary to call methods (values in dictionary) to run based on user input (key in dictionary) in a different function? 检查用户输入的格式,然后将其拆分为字典 - Checking the format of a user's input, then splitting it into a dictionary 有没有办法使用用户输入将键值从另一个字典添加到字典中 - Is there a way to add key values to a dictionary from another dictionary using user input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM