[英]How to extract a specific value(only value without its key) in a dictionary
I used python to write a dictionary of a customer:我用python写了一个客户的字典:
customer = {'name': name, 'phone_number': phone_number, 'code': code1}
the key-values of this dictionary have been defined.My trouble is:这本字典的键值已经定义。我的麻烦是:
When I want to check the value of the input_name
(the user inputs) whether is the same as the value of the key name
(in dictionary), I don't know how to extract the value of the key name
in dictionary.当我想检查
input_name
(用户输入)的值是否与键name
(在字典中)的值相同时,我不知道如何提取键name
在字典中的值。 Should I use the method .value
of a dictionary?我应该使用字典的方法
.value
吗?
Thanks for help in advance.提前感谢您的帮助。
if input_name == customer['name']:
print("yes")
else:
print("no")
You can use one of the following您可以使用以下其中一项
customer['name'] == input_name
Or if you don't know if the 'name' is actually in the dictionary或者,如果您不知道“名字”是否真的在字典中
customer.get('name', 'default_value') == input_name
Both of the above statements will evaluate to True
if the input_name
is the same as the name in dictionary, False
otherwise如果
input_name
与字典中的名称相同,则以上两个语句的计算结果为True
,否则为False
You can use您可以使用
print("Yes" if input_name == customer['name'] else "No")
You can use get method.您可以使用获取方法。
value = customer.get('name', None)
If the name doesn't exist, it will return None如果名称不存在,它将返回 None
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.