我们在 python 程序中使用字典来存储和操作键值对。有时,我们需要检查一个值是否存在于字典中。在这个 python 教程中,我们将讨论检查一个值是否存在于python 字典中的不同方法。在这里,在检查值的时候,我们可能有可用的键,也可能没有。我们将讨论在两种情况下如何检查一个值是否存在于字典中。
内容表
当我们有可用的键时,检查一个值是否存在于一个字典中
当我们有字典的键时,我们可以使用下标操作符或get() 方法来检查一个给定的值是否存在于字典中。让我们逐一讨论每种方法。
使用下标操作符检查一个值是否存在于字典中
下标操作符
当我们有一个键,并且我们想检查一个值是否存在于字典中,我们可以使用下标操作符。 为此,我们可以使用方括号来检索与键相关的值,其语法如下。
value=dict[key_val]
这里,dict 是字典的名称,key_val 是键。执行后,上述语句返回与字典中的键相关的值。你可以在下面的代码例子中观察到这一点。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
key = "name"
value = myDict[key]
print("The value associated with the key \"{}\" is \"{}\".".format(key, value))
输出。
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The value associated with the key "name" is "Python For Beginners".
在这里,我们首先创建了一个名为myDict 的字典,其键为name,url,acronym, 和type 。之后,我们使用下标操作符检索了与键 'name' 相关的值。这里,程序运行正常。
然而,在有些情况下,提供给下标运算符的键可能不存在于字典中。在这种情况下,程序将遇到KeyError 异常,如下所示。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
key = "class"
value = myDict[key]
print("The value associated with the key \"{}\" is \"{}\".".format(key, value))
输出。
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
Traceback (most recent call last):
File "/home/aditya1117/PycharmProjects/pythonProject/webscraping.py", line 5, in <module>
value = myDict[key]
KeyError: 'class'
这里,键 'class' 不存在于字典中。因此,程序会遇到 [KeyError](https://www.pythonforbeginners.com/basics/python-keyerror)异常。
在这种情况下,程序将被突然终止,在程序执行过程中完成的任何工作都将丢失。在这种情况下,你可以使用python的try-except块来处理这个异常,如下图所示。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
key = "class"
try:
value = myDict[key]
print("The value associated with the key \"{}\" is \"{}\".".format(key, value))
except KeyError:
print("The key '{}' is not present in the dictionary.".format(key))
输出。
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The key 'class' is not present in the dictionary.
在上面的例子中,KeyError 异常是在try块中引发的。在except块中,我们捕获了这个异常,并通过为用户打印适当的信息来正常终止程序。
当我们有一个单一的输入键时
要使用下标符号检查一个值是否存在于字典中,我们将获得与字典中键名相关的值。然后,我们将检查获得的值是否等于我们要检查的给定值。
如果两个值都匹配,我们就说输入值对存在于字典中。否则,不存在。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
key = "name"
input_value = "Python For Beginners"
print("Input key is:", key)
print("Input value is:", input_value)
try:
value = myDict[key]
if value == input_value:
print("'{}' is present in the dictionary".format(input_value))
else:
print("'{}' is not present in the dictionary".format(input_value))
except KeyError:
print("The key '{}' is not present in the dictionary.".format(key))
输出。
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
Input key is: name
Input value is: Python For Beginners
'Python For Beginners' is present in the dictionary
在上面的代码中,我们已经提供了键 'name'。 除此之外,我们还有一个值'Python For Beginners',我们必须检查它的存在。由于我们只有一个键,在这种情况下,我们只是获得了与给定键相关的值。之后,我们将获得的值与给定的输入值进行比较,以检查输入值是否存在于字典中。
当我们有多个输入键时
现在,我们有多个键,我们需要检查一个值是否存在于字典中。这里,我们需要对每个键执行上面讨论的整个操作。
在这种方法中,我们将使用for循环遍历作为输入的键的列表。对于列表中的每个键,我们将检索相关的值并与输入值进行比较。如果两个值都匹配,我们就说输入值存在于字典中。同时,我们将跳出 for 循环。
如果没有一个键的关联值等于输入值,我们就说这个值不存在于字典中。你可以在下面的例子中观察整个过程。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
keys = ["name", 'type']
input_value = "Python For Beginners"
print("Input keys are:", keys)
print("Input value is:", input_value)
valueFound = False
for key in keys:
try:
value = myDict[key]
if value == input_value:
print("'{}' is present in the dictionary".format(input_value))
valueFound = True
break
else:
continue
except KeyError:
print("The key '{}' is not present in the dictionary.".format(key))
if not valueFound:
print("'{}' is not present in the dictionary".format(input_value))
输出。
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
Input keys are: ['name', 'type']
Input value is: Python For Beginners
'Python For Beginners' is present in the dictionary
在使用下标运算符时,如果键不存在于字典中,程序会遇到KeyError 异常。处理KeyError 异常在时间和内存上都是很昂贵的。因此,我们可以通过使用keys() 方法检查键的存在或使用 get() 方法来避免该异常。
使用keys()方法检查一个值是否存在于一个字典中
keys() 方法,当对一个字典调用时,会返回一个包含字典中键的dict_keys 对象。你可以在下面的结果中观察到这一点。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
keys = myDict.keys()
print("The keys in the dictionary are:")
print(keys)
输出。
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The keys in the dictionary are:
dict_keys(['name', 'url', 'acronym', 'type'])
当我们有一个单一的输入键时
要用keys() 方法检查一个值是否存在于 dictionary 中,我们首先要获得 dictionary 的键的列表。
之后,我们将检查某个特定的键是否存在,以确定该键是现有字典的一个有效键。如果输入的键存在于键的列表中,我们将继续检查给定的值是否存在于字典中。
对于一个有效的键,为了检查该值是否存在于字典中,我们将获得与字典中的键相关的值。然后,我们将检查获得的值是否等于我们正在检查的给定值。如果两个值都匹配,我们就说这个值存在于字典中。否则就不存在。
你可以在下面的例子中观察到这一点。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
keys = myDict.keys()
print("The keys in the dictionary are:")
print(keys)
key="name"
input_value="Python For Beginners"
if key in keys:
value = myDict[key]
if value == input_value:
print("'{}' is present in the dictionary.".format(input_value))
else:
print("'{}' is not present in the dictionary.".format(input_value))
else:
print("The key '{}' is not present in the dictionary.".format(key))
输出。
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The keys in the dictionary are:
dict_keys(['name', 'url', 'acronym', 'type'])
'Python For Beginners' is present in the dictionary.
当我们有多个输入键时
当我们有一个以上的键时,我们可以用for循环来迭代键的列表。我们将对每个给定的键重复整个过程。如果没有一个键的相关值与给定值相同,我们就说这个值不存在于字典中。你可以在下面的例子中观察到这整个过程。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
keys = myDict.keys()
print("The keys in the dictionary are:")
print(keys)
input_keys = ['Aditya',"name", 'type']
input_value = "Python For Beginners"
valueFound = False
for key in input_keys:
if key in keys:
value = myDict[key]
if value == input_value:
print("'{}' is present in the dictionary.".format(input_value))
valueFound = True
break
else:
continue
else:
print("The key '{}' is not present in the dictionary.".format(key))
if not valueFound:
print("'{}' is not present in the dictionary.".format(input_value))
输出。
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The keys in the dictionary are:
dict_keys(['name', 'url', 'acronym', 'type'])
The key 'Aditya' is not present in the dictionary.
'Python For Beginners' is present in the dictionary.
使用 get() 方法检查一个值是否存在于字典中
我们可以使用keys() 方法来检查一个键是否存在,然后用下标操作符来获取值,而不是使用get() 方法来检查一个值是否存在于字典中。
get() 方法,当对一个 dictionary 调用时,接受一个 key 作为输入参数。如果键存在于 dictionary 中,它返回与键相关的值,如下所示。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
key = "name"
print("THe key is '{}'.".format(key))
value = myDict.get(key)
print("THe value is '{}'.".format(value))
输出。
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
THe key is 'name'.
THe value is 'Python For Beginners'.
如果给定的键不存在于字典中,get() 方法返回默认值None 。你可以在下面的例子中观察到这一点。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
key = "website"
print("THe key is '{}'.".format(key))
value = myDict.get(key)
print("THe value is '{}'.".format(value))
输出。
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
THe key is 'website'.
THe value is 'None'.
当我们有一个单一的输入键时
要使用 get 函数检查一个值是否存在于 dictionary 中,我们将获得与给定键相关的值。之后,我们将检查获得的值是否等于给定的值。如果是,我们就说这个值存在于 dictionary 中。否则,不存在。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
key = "name"
input_value = "Python For Beginners"
print("The key is '{}'.".format(key))
print("The input value is '{}'.".format(input_value))
value = myDict.get(key)
if value is None:
print("The key '{}' is not present in the dictionary.".format(key))
elif value == input_value:
print("The value '{}' is present in the dictionary.".format(input_value))
else:
print("The value '{}' is not present in the dictionary.".format(input_value))
输出。
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The key is 'name'.
The input value is 'Python For Beginners'.
The value 'Python For Beginners' is present in the dictionary.
当我们有多个输入键时
如果我们有多个键,我们可以用一个for循环来迭代键的列表。在迭代过程中,我们可以检查每个键的输入值是否存在。如果没有一个给定的键的相关值等于给定的值,我们将说这个值不存在于字典中。你可以在下面的例子中观察到这一点。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
keys = ['Aditya', "name", 'url']
input_value = "Python For Beginners"
print("The input keys are '{}'.".format(keys))
print("The input value is '{}'.".format(input_value))
valueFound=False
for key in keys:
value = myDict.get(key)
if value is None:
print("The key '{}' is not present in the dictionary.".format(key))
elif value == input_value:
print("The value '{}' is present in the dictionary.".format(input_value))
valueFound=True
break
if not valueFound:
print("The value '{}' is not present in the dictionary.".format(input_value))
输出。
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The input keys are '['Aditya', 'name', 'url']'.
The input value is 'Python For Beginners'.
The key 'Aditya' is not present in the dictionary.
The value 'Python For Beginners' is present in the dictionary.
到现在为止,我们已经讨论了当我们得到字典中的一些键时,检查一个值是否存在于字典中的不同情况。
现在让我们讨论一下,当没有给定键,只给定一个值时,我们要检查这个值是否存在于字典中的不同方法。
检查一个值是否存在于字典中,当我们的键不可用时
使用keys()方法检查一个值是否存在于字典中
要使用 keys() 方法检查一个值是否存在于字典中,我们首先要通过在字典上执行keys() 方法来获得键的列表。
之后,我们将使用下标操作符来获得与键列表中的每个键相关的 dictionary 的值。如果得到的任何一个值等于我们要检查的那个值,我们就说这个值存在于 dictionary 中。否则不存在。你可以在下面的例子中观察到这一点。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
input_value = "Python For Beginners"
print("The input value is '{}'.".format(input_value))
keys = myDict.keys()
isPresent = False
for key in keys:
value = myDict[key]
if value == input_value:
print("'{}' is present in the dictionary.".format(input_value))
isPresent = True
break
else:
continue
if not isPresent:
print("'{}' is not present in the dictionary.".format(input_value))
输出。
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The input value is 'Python For Beginners'.
'Python For Beginners' is present in the dictionary.
使用keys()方法检查多个值是否存在于一个字典中
如果我们需要检查字典中是否有多个值,我们将首先使用get() 方法和keys() 方法获得一个值的列表,如下图所示。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
keys = myDict.keys()
print("The keys of the dictionary are:")
print(keys)
values = []
for key in keys:
value = myDict.get(key)
values.append(value)
print("The obtained values are '{}'.".format(values))
输出。
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The keys of the dictionary are:
dict_keys(['name', 'url', 'acronym', 'type'])
The obtained values are '['Python For Beginners', 'pythonforbeginners.com', 'PFB', 'python blog']'.
在这里,我们首先使用keys() 方法获得了 dictionary 的 keys。之后,我们创建了一个空列表来存储字典的值。然后,我们使用get() 方法获得与字典中每个键相关的值,并将其存储在列表中。
在获得 dictionary 中的值的列表后,我们将检查每个输入值是否存在于其中。为此,我们可以使用一个带有成员运算符的for循环,如下所示。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
keys = myDict.keys()
print("The keys of the dictionary are:")
print(keys)
input_values = ["Python For Beginners", "PFB", 'Aditya']
print("The input values are:", input_values)
values = []
for key in keys:
value = myDict.get(key)
values.append(value)
print("The obtained values are '{}'.".format(values))
for value in input_values:
if value in values:
print("The value '{}' is present in the dictionary.".format(value))
else:
print("The value '{}' is not present in the dictionary.".format(value))
输出。
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The keys of the dictionary are:
dict_keys(['name', 'url', 'acronym', 'type'])
The input values are: ['Python For Beginners', 'PFB', 'Aditya']
The obtained values are '['Python For Beginners', 'pythonforbeginners.com', 'PFB', 'python blog']'.
The value 'Python For Beginners' is present in the dictionary.
The value 'PFB' is present in the dictionary.
The value 'Aditya' is not present in the dictionary.
使用 values() 方法检查一个值是否存在于字典中
values() 方法,当在一个字典上调用时,会返回一个包含字典值的dict_values 对象的副本。你可以在下面的例子中观察到这一点。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
values = myDict.values()
print("The values in the dictionary are:")
print(values)
输出。
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The values in the dictionary are:
dict_values(['Python For Beginners', 'pythonforbeginners.com', 'PFB', 'python blog'])
要使用values() 方法检查一个值是否存在于一个 dictionary 中,我们首先通过在 dictionary 上调用values() 方法获得包含 dictionary 的值的dict_values 对象。
然后,我们将遍历值的列表,检查用户输入的值是否存在于值的列表中。如果是,我们将说这个值存在于 dictionary 中。否则不存在。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
values = myDict.values()
print("The values in the dictionary are:")
print(values)
input_value = "Python For Beginners"
print("The input value is '{}'.".format(input_value))
isPresent = False
for value in values:
if value == input_value:
print("'{}' is present in the dictionary.".format(input_value))
isPresent = True
break
else:
continue
if not isPresent:
print("'{}' is not present in the dictionary.".format(input_value))
输出。
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The values in the dictionary are:
dict_values(['Python For Beginners', 'pythonforbeginners.com', 'PFB', 'python blog'])
The input value is 'Python For Beginners'.
'Python For Beginners' is present in the dictionary.
我们可以使用成员操作符 "in" 来检查给定的值是否存在于值的列表中,而不是使用 for 循环来遍历值的列表。in 操作符的语法如下。
element in container_object
in 运算符是一个二进制运算符,它把一个元素作为它的第一个操作数,把一个容器对象或一个迭代器作为它的第二个操作数。执行后,如果该元素存在于容器对象或迭代器中,它将返回True 。否则,它返回False 。
为了检查给定的值是否存在于字典中,我们将使用成员操作符检查该值是否存在于values() 方法返回的值列表中,如下面的例子所示。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
values = myDict.values()
print("The values in the dictionary are:")
print(values)
input_value = "Python For Beginners"
print("The input value is '{}'.".format(input_value))
if input_value in values:
print("'{}' is present in the dictionary.".format(input_value))
else:
print("'{}' is not present in the dictionary.".format(input_value))
输出。
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The values in the dictionary are:
dict_values(['Python For Beginners', 'pythonforbeginners.com', 'PFB', 'python blog'])
The input value is 'Python For Beginners'.
'Python For Beginners' is present in the dictionary.
使用values()方法检查多个值是否存在于一个字典中
如果我们被赋予多个值来检查它们是否存在,我们将使用成员操作符的for循环和values() 方法来检查键的存在。在这里,我们将使用for循环遍历输入值的列表。在迭代过程中,我们将检查当前值是否存在于使用values() 方法得到的值列表中。如果是,我们将打印该值存在于字典中。否则不存在。你可以在下面的例子中观察到这一点。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
input_values = ["Python For Beginners", "PFB", 'Aditya']
print("The input values are:", input_values)
values = myDict.values()
for value in input_values:
if value in values:
print("The value '{}' is present in the dictionary.".format(value))
else:
print("The value '{}' is not present in the dictionary.".format(value))
输出。
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The input values are: ['Python For Beginners', 'PFB', 'Aditya']
The value 'Python For Beginners' is present in the dictionary.
The value 'PFB' is present in the dictionary.
The value 'Aditya' is not present in the dictionary.
使用 viewvalues() 方法检查一个值是否存在于字典中
如果您使用的是 Python 2.x 版本,您可以使用 values() 方法,而不是使用viewvalues() 方法来检查一个值是否存在于字典中。
viewvalues() 方法,当在一个 dictionary 上调用时,会返回一个dict_values 对象的视图,其中包含 dictionary 中值的新视图,如下所示。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
values = myDict.viewvalues()
print("The values in the dictionary are:")
print(values)
输出。
The dictionary is:
{'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog', 'name': 'Python For Beginners'}
The values in the dictionary are:
dict_values(['pythonforbeginners.com', 'PFB', 'python blog', 'Python For Beginners'])
获得dict_values 对象后,我们可以检查输入值是否存在于字典中。
要使用viewvalues() 方法检查一个值是否存在于 dictionary 中,我们将首先通过对 dictionary 调用viewvalues() 方法来获得dict_values 对象。
之后,我们将遍历dict_values 对象,检查作为用户输入的值是否存在于值列表中。如果是,我们将说该值存在于字典中。否则不存在。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
values = myDict.viewvalues()
print("The values in the dictionary are:")
print(values)
input_value = "Python For Beginners"
print("The input value is '{}'.".format(input_value))
isPresent = False
for value in values:
if value == input_value:
print("'{}' is present in the dictionary.".format(input_value))
isPresent = True
break
else:
continue
if not isPresent:
print("'{}' is not present in the dictionary.".format(input_value))
输出。
The dictionary is:
{'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog', 'name': 'Python For Beginners'}
The values in the dictionary are:
dict_values(['pythonforbeginners.com', 'PFB', 'python blog', 'Python For Beginners'])
The input value is 'Python For Beginners'.
'Python For Beginners' is present in the dictionary.
我们也可以使用成员测试来检查输入值是否存在于dict_values 对象中,而不是使用 for 循环,如下所示。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
values = myDict.viewvalues()
print("The values in the dictionary are:")
print(values)
input_value = "Python For Beginners"
print("The input value is '{}'.".format(input_value))
if input_value in values:
print("'{}' is present in the dictionary.".format(input_value))
else:
print("'{}' is not present in the dictionary.".format(input_value))
输出。
The dictionary is:
{'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog', 'name': 'Python For Beginners'}
The values in the dictionary are:
dict_values(['pythonforbeginners.com', 'PFB', 'python blog', 'Python For Beginners'])
The input value is 'Python For Beginners'.
'Python For Beginners' is present in the dictionary.
使用viewvalues()方法检查字典中是否存在多个值
如果我们被赋予多个值来检查它们是否存在,我们将使用for循环与成员操作符和viewvalues() 方法来检查键的存在。在这里,我们将使用for循环遍历输入值的列表。在迭代过程中,我们将检查当前值是否存在于使用viewvalues() 方法获得的值列表中。如果是,我们将打印该值存在于字典中。否则不存在。你可以在下面的例子中观察到这一点。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
values = myDict.viewvalues()
print("The values in the dictionary are:")
print(values)
input_values = ["Python For Beginners",'PFB','Aditya']
print("The input values are '{}'.".format(input_values))
for input_value in input_values:
if input_value in values:
print("'{}' is present in the dictionary.".format(input_value))
else:
print("'{}' is not present in the dictionary.".format(input_value))
输出。
The dictionary is:
{'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog', 'name': 'Python For Beginners'}
The values in the dictionary are:
dict_values(['pythonforbeginners.com', 'PFB', 'python blog', 'Python For Beginners'])
The input values are '['Python For Beginners', 'PFB', 'Aditya']'.
'Python For Beginners' is present in the dictionary.
'PFB' is present in the dictionary.
'Aditya' is not present in the dictionary.
使用 itervalues() 方法检查一个值是否存在于字典中
在 python 2 中,我们也可以使用 itervalues() 方法来检查一个值是否存在于字典中。
itervalues() 方法,当在一个 dictionary 上调用时,返回一个迭代器,我们可以用它来迭代 dictionary 中的值。
要使用itervalues() 方法检查一个值是否存在于一个 dictionary 中,我们首先要在 dictionary 上调用 itervalues() 方法来获得它所返回的迭代器。之后,我们可以用 for 循环遍历这个迭代器,检查输入值是否存在于迭代器中。如果是,我们就说这个值存在于 dictionary 中。否则不存在。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
values = myDict.itervalues()
input_value = "Python For Beginners"
print("The input value is '{}'.".format(input_value))
isPresent = False
for value in values:
if value == input_value:
print("'{}' is present in the dictionary.".format(input_value))
isPresent = True
break
else:
continue
if not isPresent:
print("'{}' is not present in the dictionary.".format(input_value))
输出。
The dictionary is:
{'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog', 'name': 'Python For Beginners'}
The input value is 'Python For Beginners'.
'Python For Beginners' is present in the dictionary.
不使用 for 循环,我们也可以使用成员测试来检查输入值是否存在于dict_values 对象中,如下所示。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
values = myDict.itervalues()
input_value = "Python For Beginners"
print("The input value is '{}'.".format(input_value))
if input_value in values:
print("'{}' is present in the dictionary.".format(input_value))
else:
print("'{}' is not present in the dictionary.".format(input_value))
输出。
The dictionary is:
{'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog', 'name': 'Python For Beginners'}
The input value is 'Python For Beginners'.
'Python For Beginners' is present in the dictionary.
使用 itervalues() 方法检查字典中是否存在多个值
如果我们被赋予多个值来检查它们是否存在,我们将使用for循环与成员资格操作符和itervalues() 方法来检查键的存在。
在这里,我们将使用for循环来迭代输入值列表。在迭代过程中,我们将检查当前值是否存在于使用itervalues() 方法获得的值的迭代器中。如果是,我们将打印该值存在于字典中。否则不存在。你可以在下面的例子中观察到这一点。
myDict = {"name": "Python For Beginners", "url": "pythonforbeginners.com", "acronym": "PFB", "type": "python blog"}
print("The dictionary is:")
print(myDict)
values = myDict.itervalues()
input_values = ["Python For Beginners",'PFB','Aditya']
print("The input values are '{}'.".format(input_values))
for input_value in input_values:
if input_value in values:
print("'{}' is present in the dictionary.".format(input_value))
else:
print("'{}' is not present in the dictionary.".format(input_value))
输出。
The dictionary is:
{'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog', 'name': 'Python For Beginners'}
The input values are '['Python For Beginners', 'PFB', 'Aditya']'.
'Python For Beginners' is present in the dictionary.
'PFB' is not present in the dictionary.
'Aditya' is not present in the dictionary.
结论
在这篇文章中,我们讨论了各种方法来检查一个值是否存在于字典中。如果你有字典的键,你可以用get() 的方法来检查一个值是否存在于字典中。如果你没有键,你应该使用 python 3.x 中使用values() 方法的方法。对于 Python 2.x 版本,你应该使用itervalues() 方法,因为它是所有方法中最快的。
我希望你喜欢阅读这篇文章。要了解更多关于Python编程的信息,你可以阅读这篇文章:如何在Python中删除一个列表中出现的所有字符。你可能也喜欢这篇文章:如何检查一个Python字符串是否包含一个数字。
请继续关注更多内容丰富的文章。
学习愉快
The postCheck if Value Exists in a Dictionary in Pythonappeared first onPythonForBeginners.com.