Python kiểm tra xem giá trị có trong từ điển không

Đôi khi, khi làm việc với từ điển Python, chúng tôi có một trường hợp sử dụng cụ thể mà chúng tôi chỉ cần tìm xem liệu một giá trị cụ thể có trong từ điển hay không vì đó là giá trị của bất kỳ khóa nào. Điều này có thể có các trường hợp sử dụng trong bất kỳ lĩnh vực lập trình nào mà người ta có thể nghĩ đến. Hãy thảo luận về những cách nhất định để giải quyết vấn đề này bằng Python.  

Kiểm tra xem một giá trị có tồn tại trong từ điển hay không bằng hàm any()

Đây là phương pháp mà vấn đề này có thể được giải quyết. Trong phần này, chúng tôi lặp lại toàn bộ từ điển bằng cách sử dụng khả năng hiểu danh sách và kiểm tra các giá trị của từng khóa để khớp bằng cách sử dụng câu lệnh có điều kiện.  

Python3




test_dict= {'gfg' :1,'is' :

The original dictionary is : {'best': 3, 'is': 2, 'gfg': 1}
Is 3 present in dictionary : True
1,
The original dictionary is : {'best': 3, 'is': 2, 'gfg': 1}
Is 3 present in dictionary : True
3 :
The original dictionary is : {'best': 3, 'is': 2, 'gfg': 1}
Is 3 present in dictionary : True
5
The original dictionary is : {'best': 3, 'is': 2, 'gfg': 1}
Is 3 present in dictionary : True
6

Chúng tôi sử dụng từ điển để lưu trữ và thao tác các cặp khóa-giá trị trong chương trình python. Đôi khi, chúng ta cần kiểm tra xem một giá trị có tồn tại trong từ điển hay không. Trong hướng dẫn về python này, chúng ta sẽ thảo luận về các cách khác nhau để kiểm tra xem một giá trị có tồn tại trong từ điển python hay không. Ở đây, trong khi kiểm tra các giá trị, chúng tôi có thể có sẵn các khóa hoặc cách khác. Chúng tôi sẽ thảo luận về cách chúng tôi có thể kiểm tra xem một giá trị có trong từ điển hay không trong cả hai trường hợp

Mục lục

Kiểm tra xem một giá trị có tồn tại trong từ điển hay không khi chúng tôi có sẵn khóa

Khi chúng ta có các khóa của từ điển, chúng ta có thể sử dụng toán tử chỉ số dưới hoặc phương thức

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))
4 để kiểm tra xem một giá trị đã cho có tồn tại trong từ điển hay không. Hãy để chúng tôi thảo luận từng phương pháp một

Kiểm tra xem Giá trị có tồn tại trong Từ điển hay không bằng Toán tử chỉ số

Toán tử chỉ số

Khi chúng ta có một khóa và chúng ta muốn kiểm tra xem một giá trị có tồn tại trong từ điển hay không, chúng ta có thể sử dụng toán tử chỉ số dưới. Đối với điều này, chúng ta có thể sử dụng dấu ngoặc vuông để truy xuất giá trị được liên kết với khóa bằng cú pháp sau

Python kiểm tra xem giá trị có trong từ điển không

value=dict[key_val]

Ở đây,

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))
5 là tên của từ điển và
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))
6 là khóa. Sau khi thực hiện, câu lệnh trên trả về giá trị gắn với key trong từ điển. Bạn có thể quan sát điều này trong ví dụ mã sau

________số 8_______

đầu ra

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".

Ở đây, trước tiên chúng tôi đã tạo một tên từ điển

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))
7 với các khóa
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))
8,
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))
9,
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The key 'class' is not present in the dictionary.
0 và
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The key 'class' is not present in the dictionary.
1. Sau đó, chúng tôi đã truy xuất giá trị được liên kết với khóa '
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))
8' bằng cách sử dụng toán tử chỉ số. Tại đây, chương trình hoạt động bình thường

Tuy nhiên, có thể có những trường hợp khóa được cung cấp cho toán tử chỉ số dưới có thể không có trong từ điển. Trong trường hợp như vậy, chương trình sẽ chạy vào ngoại lệ

The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The key 'class' is not present in the dictionary.
3 như hình bên dưới

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))

đầu ra

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'

Ở đây, từ khóa ‘

The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The key 'class' is not present in the dictionary.
4‘ không tồn tại trong từ điển. Do đó, chương trình chạy vào ngoại lệ
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The key 'class' is not present in the dictionary.
5

Trong trường hợp như vậy, chương trình sẽ bị chấm dứt đột ngột và mọi công việc được thực hiện trong quá trình thực thi chương trình sẽ bị mất. Trong những trường hợp này, bạn có thể sử dụng các khối python try-except để xử lý ngoại lệ như hình bên dưới

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))

đầu ra

The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The key 'class' is not present in the dictionary.

Trong ví dụ trên, ngoại lệ

The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The key 'class' is not present in the dictionary.
3 được đưa ra trong khối thử. Trong khối ngoại trừ, chúng tôi bắt ngoại lệ và kết thúc chương trình bình thường bằng cách in thông báo thích hợp cho người dùng

Khi chúng tôi có một khóa đầu vào duy nhất

Để kiểm tra xem một giá trị có tồn tại trong từ điển hay không bằng cách sử dụng ký hiệu chỉ số dưới, chúng tôi sẽ lấy giá trị được liên kết với tên khóa trong từ điển. Sau đó, chúng tôi sẽ kiểm tra xem giá trị thu được có bằng giá trị đã cho mà chúng tôi đang kiểm tra hay không

Nếu cả hai giá trị khớp nhau, chúng tôi sẽ nói rằng cặp giá trị đầu vào có trong từ điển. Nếu không thì không

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))

đầu ra

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

Chúng tôi đã được cung cấp khóa '______4_______8' trong đoạn mã trên. Cùng với đó, chúng tôi có giá trị

The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The key 'class' is not present in the dictionary.
8‘ mà chúng tôi phải kiểm tra sự hiện diện của nó. Vì chúng tôi chỉ có một khóa, trong trường hợp này, chúng tôi vừa nhận được giá trị được liên kết với khóa đã cho. Sau đó, chúng tôi so sánh giá trị thu được với giá trị đầu vào đã cho để kiểm tra xem giá trị đầu vào có tồn tại trong từ điển hay không

Khi chúng ta có nhiều phím nhập liệu

Bây giờ, chúng tôi có nhiều khóa và chúng tôi cần kiểm tra xem giá trị có tồn tại trong từ điển không. Ở đây, chúng ta cần thực hiện toàn bộ thao tác đã thảo luận ở trên cho từng phím

Theo cách tiếp cận này, chúng tôi sẽ lặp lại danh sách các khóa được cung cấp làm đầu vào bằng vòng lặp for. Đối với mỗi khóa có trong danh sách, chúng tôi sẽ lấy giá trị được liên kết và so sánh nó với giá trị đầu vào. Nếu cả hai giá trị khớp nhau, chúng tôi sẽ nói rằng giá trị đầu vào có trong từ điển. Đồng thời, chúng tôi sẽ thoát ra khỏi vòng lặp for

Nếu không có khóa nào có giá trị được liên kết bằng với giá trị đầu vào, chúng tôi sẽ nói rằng giá trị đó không có trong từ điển. Bạn có thể quan sát toàn bộ quá trình trong ví dụ sau

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))

đầu ra

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))
0

Trong khi sử dụng toán tử chỉ số dưới, chương trình gặp phải ngoại lệ

The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The key 'class' is not present in the dictionary.
3 nếu khóa không tồn tại trong từ điển. Xử lý ngoại lệ
The dictionary is:
{'name': 'Python For Beginners', 'url': 'pythonforbeginners.com', 'acronym': 'PFB', 'type': 'python blog'}
The key 'class' is not present in the dictionary.
3 tốn kém về thời gian và bộ nhớ. Do đó, chúng ta có thể tránh ngoại lệ bằng cách kiểm tra sự hiện diện của khóa bằng cách sử dụng phương pháp
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))
1 hoặc bằng cách sử dụng phương pháp_______4_______4

Kiểm tra xem Giá trị có tồn tại trong Từ điển hay không bằng cách sử dụng phương thức keys()

Phương thức ____24_______1, khi được gọi trên từ điển, sẽ trả về một đối tượng

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))
4 chứa các khóa của từ điển. Bạn có thể quan sát điều này trong kết quả sau

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))
1

đầu ra

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))
2

Khi chúng tôi có một khóa đầu vào duy nhất

Để kiểm tra xem một giá trị có tồn tại trong từ điển hay không bằng cách sử dụng phương pháp

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))
1, trước tiên chúng ta sẽ lấy danh sách các khóa của từ điển

Sau đó, chúng tôi sẽ kiểm tra sự hiện diện của một khóa cụ thể để xác định rằng khóa đó là khóa hợp lệ cho từ điển hiện có. Nếu khóa đầu vào có trong danh sách các khóa, chúng tôi sẽ tiến hành kiểm tra xem giá trị đã cho có tồn tại trong từ điển hay không

Đối với key hợp lệ, để kiểm tra xem giá trị đó có tồn tại trong từ điển hay không, chúng ta sẽ lấy giá trị tương ứng với key của từ điển. Sau đó, chúng tôi sẽ kiểm tra xem giá trị thu được có bằng giá trị đã cho mà chúng tôi đang kiểm tra hay không. Nếu cả hai giá trị khớp nhau, chúng tôi sẽ nói rằng giá trị đó có trong từ điển. Nếu không thì không

Bạn có thể quan sát điều này trong ví dụ sau

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))
3

đầu ra

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))
4

Khi chúng ta có nhiều phím nhập liệu

Khi chúng ta có nhiều hơn một khóa, chúng ta có thể sử dụng vòng lặp for để lặp qua danh sách các khóa. Chúng tôi sẽ lặp lại toàn bộ quy trình cho từng khóa đã cho. Nếu không có khóa nào có giá trị được liên kết giống với giá trị đã cho, chúng tôi sẽ nói rằng giá trị đó không có trong từ điển. Bạn có thể quan sát toàn bộ quá trình này trong ví dụ dưới đây

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))
5

đầu ra

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))
6

Kiểm tra xem Giá trị có tồn tại trong Từ điển hay không bằng phương thức get()

Thay vì sử dụng phương pháp

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))
1 để kiểm tra sự tồn tại của khóa và sau đó lấy giá trị bằng toán tử chỉ số dưới, chúng ta có thể sử dụng phương pháp
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))
4 để kiểm tra xem một giá trị có trong từ điển hay không

Phương thức

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))
4, khi được gọi trên từ điển, chấp nhận một khóa làm đối số đầu vào. Nếu khóa có trong từ điển, nó sẽ trả về giá trị được liên kết với khóa như hình bên dưới

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))
7

đầu ra

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))
8

Nếu khóa đã cho không có trong từ điển, phương thức

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))
4 trả về giá trị mặc định
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
0. Bạn có thể quan sát điều này trong ví dụ sau

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))
9

đầu ra

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".
0

Khi chúng tôi có một khóa đầu vào duy nhất

Để kiểm tra xem một giá trị có tồn tại trong từ điển hay không bằng hàm get, chúng ta sẽ lấy giá trị được liên kết với khóa đã cho. Sau đó, chúng ta sẽ kiểm tra xem giá trị thu được có bằng giá trị đã cho hay không. Nếu có, chúng tôi sẽ nói rằng giá trị đó có trong từ điển. Nếu không thì không

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".
1

đầu ra

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".
2

Khi chúng ta có nhiều phím nhập liệu

Nếu chúng tôi được cung cấp nhiều khóa, chúng tôi có thể sử dụng vòng lặp for để lặp qua danh sách các khóa. Trong khi lặp lại, chúng ta có thể kiểm tra xem giá trị đầu vào có xuất hiện cho mỗi khóa hay không. Nếu Không có khóa nào có giá trị được liên kết bằng với giá trị đã cho, chúng tôi sẽ nói rằng giá trị đó không có trong từ điển. Bạn có thể quan sát điều này trong ví dụ sau

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".
3

đầu ra

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".
4

Cho đến bây giờ, chúng ta đã thảo luận về các tình huống khác nhau để kiểm tra xem một giá trị có tồn tại trong từ điển hay không khi chúng ta được cung cấp một số khóa của từ điển

Bây giờ chúng ta hãy thảo luận về các cách khác nhau để kiểm tra xem một giá trị có tồn tại trong từ điển hay không khi các khóa không được cung cấp và chỉ một giá trị được cung cấp mà chúng ta phải kiểm tra sự hiện diện của nó

Kiểm tra xem một giá trị có tồn tại trong từ điển khi chúng tôi không có khóa

Kiểm tra xem Giá trị có tồn tại trong Từ điển hay không bằng cách sử dụng phương thức keys()

Để kiểm tra xem một giá trị có tồn tại trong từ điển hay không bằng cách sử dụng phương pháp_______24_______1, trước tiên chúng tôi sẽ lấy danh sách các khóa bằng cách thực hiện phương thức

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))
1 trên từ điển

Sau đó, chúng tôi sẽ sử dụng toán tử chỉ số để lấy các giá trị của từ điển được liên kết với từng khóa có trong danh sách các khóa. Nếu bất kỳ giá trị nào thu được bằng với giá trị mà chúng tôi đang kiểm tra, chúng tôi sẽ nói rằng giá trị đó có trong từ điển. Nếu không thì không. Bạn có thể quan sát điều này trong ví dụ sau

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".
5

đầu ra

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".
6

Kiểm tra xem có tồn tại nhiều giá trị trong từ điển hay không bằng cách sử dụng phương thức keys()

Nếu chúng ta cần kiểm tra xem có nhiều giá trị trong từ điển hay không, trước tiên chúng ta sẽ lấy danh sách các giá trị bằng cách sử dụng phương thức

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))
4 và phương thức
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))
1 như hình bên dưới

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".
7

đầu ra

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".
8

Ở đây, lần đầu tiên chúng tôi lấy được các khóa của từ điển bằng phương pháp

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))
1. Sau đó, chúng tôi đã tạo một danh sách trống để lưu trữ các giá trị của từ điển. Sau đó, chúng tôi thu được giá trị được liên kết với từng khóa trong từ điển bằng cách sử dụng phương pháp
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))
4 và lưu trữ nó trong danh sách

Sau khi có được danh sách các giá trị trong từ điển, chúng tôi sẽ kiểm tra xem từng giá trị đầu vào có trong đó không. Đối với điều này, chúng ta có thể sử dụng vòng lặp for với toán tử thành viên như hình bên dưới

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".
9

đầu ra

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))
0

Kiểm tra xem Giá trị có tồn tại trong Từ điển hay không bằng Phương thức values()

Phương thức

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
7, khi được gọi trên từ điển, sẽ trả về một bản sao của đối tượng
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
8 chứa các giá trị của từ điển. Bạn có thể quan sát điều này trong ví dụ sau.  

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))
1

đầu ra

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))
2

Để kiểm tra xem một giá trị có tồn tại trong từ điển hay không bằng cách sử dụng phương thức

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
7, trước tiên chúng ta sẽ lấy đối tượng
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
8 chứa các giá trị của từ điển bằng cách gọi phương thức
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
7 trên từ điển

Sau đó, chúng tôi sẽ lặp qua danh sách các giá trị để kiểm tra xem giá trị được cung cấp khi người dùng nhập liệu có trong danh sách giá trị hay không. Nếu có, chúng tôi sẽ nói rằng giá trị đó có trong từ điển. Nếu không thì không

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))
3

đầu ra

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))
4

Thay vì sử dụng vòng lặp for để lặp qua danh sách các giá trị, chúng ta có thể sử dụng toán tử thành viên “

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))
2” để kiểm tra xem giá trị đã cho có trong danh sách giá trị hay không. Cú pháp của toán tử
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))
2 như sau

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))
5

Toán tử

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))
2 là một toán tử nhị phân lấy một phần tử làm toán hạng đầu tiên của nó và một đối tượng vùng chứa hoặc một trình vòng lặp làm toán hạng thứ hai của nó. Sau khi thực thi, nó trả về
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))
5 nếu phần tử có trong đối tượng vùng chứa hoặc trình vòng lặp. Nếu không, nó sẽ trả về
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))
6

Để kiểm tra xem giá trị đã cho có tồn tại trong từ điển hay không, chúng ta sẽ kiểm tra xem giá trị đó có tồn tại trong danh sách các giá trị được trả về bởi phương thức

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
7 hay không bằng cách sử dụng toán tử thành viên như trong ví dụ sau

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))
6

đầu ra

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))
4

Kiểm tra xem có tồn tại nhiều giá trị trong một từ điển hay không bằng cách sử dụng phương thức values()

Nếu chúng tôi được cung cấp nhiều giá trị để kiểm tra sự hiện diện của chúng, chúng tôi sẽ sử dụng vòng lặp for với toán tử thành viên và phương thức

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
7 để kiểm tra sự hiện diện của các khóa. Ở đây, chúng tôi sẽ lặp lại danh sách các giá trị đầu vào bằng vòng lặp for. Trong khi lặp lại, chúng tôi sẽ kiểm tra xem giá trị hiện tại có trong danh sách các giá trị thu được bằng phương thức
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
7 hay không. Nếu có, chúng tôi sẽ in giá trị đó có trong từ điển. Nếu không thì không. Bạn có thể quan sát điều này trong ví dụ sau

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))
8

đầu ra

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))
9

Kiểm tra xem Giá trị có tồn tại trong Từ điển hay không bằng Phương thức viewvalues()

Nếu bạn đang sử dụng Python phiên bản 2. x, thay vì sử dụng phương thức

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
7, bạn có thể sử dụng phương thức
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))
01 để kiểm tra xem một giá trị có tồn tại trong từ điển hay không.  

Phương thức

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))
01, khi được gọi trên từ điển, sẽ trả về dạng xem của đối tượng
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
8 chứa dạng xem mới về các giá trị trong từ điển như hình bên dưới

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'
0

đầu ra

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'
1

Sau khi lấy được đối tượng

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
8, chúng ta có thể kiểm tra xem giá trị nhập vào có tồn tại trong từ điển hay không.  

Để kiểm tra xem một giá trị có tồn tại trong từ điển hay không bằng cách sử dụng phương thức

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))
01, trước tiên chúng ta sẽ lấy đối tượng
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
8 bằng cách gọi phương thức
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))
01 trên từ điển.  

Sau đó, chúng tôi sẽ lặp qua đối tượng

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
8 để kiểm tra xem giá trị được cung cấp khi người dùng nhập liệu có trong danh sách giá trị hay không. Nếu có, chúng tôi sẽ nói rằng giá trị đó có trong từ điển. Nếu không thì không

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'
2

đầu ra

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'
3

Thay vì sử dụng vòng lặp for, chúng ta cũng có thể kiểm tra sự hiện diện của giá trị đầu vào trong đối tượng

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
8 bằng cách sử dụng kiểm tra thành viên như bên dưới

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'
4

đầu ra

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'
3

Kiểm tra xem có tồn tại nhiều giá trị trong từ điển hay không bằng cách sử dụng phương thức viewvalues()

Nếu chúng tôi được cung cấp nhiều giá trị để kiểm tra sự hiện diện của chúng, chúng tôi sẽ sử dụng vòng lặp for với toán tử thành viên và phương thức

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))
01 để kiểm tra sự hiện diện của các khóa. Ở đây, chúng tôi sẽ lặp lại danh sách các giá trị đầu vào bằng vòng lặp for. Trong khi lặp lại, chúng tôi sẽ kiểm tra xem giá trị hiện tại có trong danh sách các giá trị thu được bằng phương thức
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))
01 hay không. Nếu có, chúng tôi sẽ in giá trị đó có trong từ điển. Nếu không thì không. Bạn có thể quan sát điều này trong ví dụ sau

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'
6

đầu ra

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'
7

Kiểm tra xem Giá trị có tồn tại trong Từ điển hay không bằng Phương thức itervalues()

Trong python 2, chúng ta cũng có thể sử dụng phương thức itervalues() để kiểm tra xem một giá trị có tồn tại trong từ điển hay không.  

Phương thức

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))
12, khi được gọi trên từ điển, sẽ trả về một trình vòng lặp mà chúng ta có thể lặp lại các giá trị trong từ điển

Để kiểm tra xem một giá trị có tồn tại trong từ điển hay không bằng cách sử dụng phương thức

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))
12, trước tiên chúng ta sẽ lấy iterator được trả về bởi phương thức_______8_______12 bằng cách gọi nó trên từ điển. Sau đó, chúng ta có thể lặp qua iterator bằng vòng lặp for và kiểm tra xem giá trị đầu vào có trong iterator hay không. Nếu có, chúng tôi sẽ nói rằng giá trị đó có trong từ điển. Nếu không thì không

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'
8

đầu ra

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'
9

Thay vì sử dụng vòng lặp for, chúng ta cũng có thể kiểm tra sự hiện diện của giá trị đầu vào trong đối tượng

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
8 bằng cách sử dụng kiểm tra thành viên như bên dưới

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))
0

đầu ra

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))
1

Kiểm tra xem có tồn tại nhiều giá trị trong từ điển hay không bằng phương thức itervalues()

Nếu chúng tôi được cung cấp nhiều giá trị để kiểm tra sự hiện diện của chúng, chúng tôi sẽ sử dụng vòng lặp for với toán tử thành viên và phương thức

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))
12 để kiểm tra sự hiện diện của các khóa

Ở đây, chúng tôi sẽ lặp lại danh sách các giá trị đầu vào bằng vòng lặp for. Trong khi lặp lại, chúng tôi sẽ kiểm tra xem giá trị hiện tại có trong trình lặp của các giá trị thu được bằng cách sử dụng phương pháp

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))
12 hay không. Nếu có, chúng tôi sẽ in giá trị đó có trong từ điển. Nếu không thì không. Bạn có thể quan sát điều này trong ví dụ sau

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))
2

đầu ra

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))
3

Phần kết luận

Trong bài viết này, chúng ta đã thảo luận về các phương pháp khác nhau để kiểm tra xem một giá trị có trong từ điển hay không. Nếu bạn có các khóa của từ điển, bạn có thể sử dụng phương pháp sử dụng phương pháp

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))
4 để kiểm tra xem một giá trị có tồn tại trong từ điển hay không. Nếu bạn không có khóa, bạn nên sử dụng phương pháp sử dụng phương thức
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
7 trong python 3. x. Đối với Python phiên bản 2. x, bạn nên sử dụng phương pháp với phương pháp
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))
12 vì đây là phương pháp nhanh nhất trong số tất cả các phương pháp

Tôi hy vọng bạn thích đọc bài viết này. Để tìm hiểu thêm về lập trình python, bạn có thể đọc bài viết này về cách loại bỏ tất cả các lần xuất hiện của một ký tự trong danh sách bằng Python. Bạn cũng có thể thích bài viết này về cách kiểm tra xem chuỗi python có chứa số không

Hãy theo dõi để biết thêm các bài viết thông tin

học tập vui vẻ

Có liên quan

Đào tạo Python được đề xuất

Khóa học. Python 3 cho người mới bắt đầu

Hơn 15 giờ nội dung video với hướng dẫn có hướng dẫn cho người mới bắt đầu. Tìm hiểu cách tạo các ứng dụng trong thế giới thực và nắm vững kiến ​​thức cơ bản