Kiểm tra xem một khóa nhất định đã tồn tại trong từ điển
Để có ý tưởng làm thế nào để làm điều đó trước tiên chúng tôi kiểm tra những phương pháp nào chúng tôi có thể gọi trên từ điển.
Đây là các phương pháp:
d={'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} Python Dictionary clear() Removes all Items Python Dictionary copy() Returns Shallow Copy of a Dictionary Python Dictionary fromkeys() Creates dictionary from given sequence Python Dictionary get() Returns Value of The Key Python Dictionary items() Returns view of dictionary (key, value) pair Python Dictionary keys() Returns View Object of All Keys Python Dictionary pop() Removes and returns element having given key Python Dictionary popitem() Returns & Removes Element From Dictionary Python Dictionary setdefault() Inserts Key With a Value if Key is not Present Python Dictionary update() Updates the Dictionary Python Dictionary values() Returns view of all values in dictionaryPhương pháp tàn bạo để kiểm tra xem khóa đã tồn tại có thể là phương pháp Python Dictionary clear() Removes all Items Python Dictionary copy() Returns Shallow Copy of a Dictionary Python Dictionary fromkeys() Creates dictionary from given sequence Python Dictionary get() Returns Value of The Key Python Dictionary items() Returns view of dictionary (key, value) pair Python Dictionary keys() Returns View Object of All Keys Python Dictionary pop() Removes and returns element having given key Python Dictionary popitem() Returns & Removes Element From Dictionary Python Dictionary setdefault() Inserts Key With a Value if Key is not Present Python Dictionary update() Updates the Dictionary Python Dictionary values() Returns view of all values in dictionary 2 không:
d.get("key")Hai phương pháp thú vị khác Python Dictionary clear() Removes all Items Python Dictionary copy() Returns Shallow Copy of a Dictionary Python Dictionary fromkeys() Creates dictionary from given sequence Python Dictionary get() Returns Value of The Key Python Dictionary items() Returns view of dictionary (key, value) pair Python Dictionary keys() Returns View Object of All Keys Python Dictionary pop() Removes and returns element having given key Python Dictionary popitem() Returns & Removes Element From Dictionary Python Dictionary setdefault() Inserts Key With a Value if Key is not Present Python Dictionary update() Updates the Dictionary Python Dictionary values() Returns view of all values in dictionary 3 và Python Dictionary clear() Removes all Items Python Dictionary copy() Returns Shallow Copy of a Dictionary Python Dictionary fromkeys() Creates dictionary from given sequence Python Dictionary get() Returns Value of The Key Python Dictionary items() Returns view of dictionary (key, value) pair Python Dictionary keys() Returns View Object of All Keys Python Dictionary pop() Removes and returns element having given key Python Dictionary popitem() Returns & Removes Element From Dictionary Python Dictionary setdefault() Inserts Key With a Value if Key is not Present Python Dictionary update() Updates the Dictionary Python Dictionary values() Returns view of all values in dictionary 4 nghe có vẻ quá nhiều công việc. Vì vậy, hãy xem xét nếu Python Dictionary clear() Removes all Items Python Dictionary copy() Returns Shallow Copy of a Dictionary Python Dictionary fromkeys() Creates dictionary from given sequence Python Dictionary get() Returns Value of The Key Python Dictionary items() Returns view of dictionary (key, value) pair Python Dictionary keys() Returns View Object of All Keys Python Dictionary pop() Removes and returns element having given key Python Dictionary popitem() Returns & Removes Element From Dictionary Python Dictionary setdefault() Inserts Key With a Value if Key is not Present Python Dictionary update() Updates the Dictionary Python Dictionary values() Returns view of all values in dictionary 2 là phương pháp phù hợp với chúng tôi. Chúng tôi có Dict Python Dictionary clear() Removes all Items Python Dictionary copy() Returns Shallow Copy of a Dictionary Python Dictionary fromkeys() Creates dictionary from given sequence Python Dictionary get() Returns Value of The Key Python Dictionary items() Returns view of dictionary (key, value) pair Python Dictionary keys() Returns View Object of All Keys Python Dictionary pop() Removes and returns element having given key Python Dictionary popitem() Returns & Removes Element From Dictionary Python Dictionary setdefault() Inserts Key With a Value if Key is not Present Python Dictionary update() Updates the Dictionary Python Dictionary values() Returns view of all values in dictionary 6 của chúng tôi:
d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10}In hiển thị khóa mà chúng tôi không có sẽ trả về Python Dictionary clear() Removes all Items Python Dictionary copy() Returns Shallow Copy of a Dictionary Python Dictionary fromkeys() Creates dictionary from given sequence Python Dictionary get() Returns Value of The Key Python Dictionary items() Returns view of dictionary (key, value) pair Python Dictionary keys() Returns View Object of All Keys Python Dictionary pop() Removes and returns element having given key Python Dictionary popitem() Returns & Removes Element From Dictionary Python Dictionary setdefault() Inserts Key With a Value if Key is not Present Python Dictionary update() Updates the Dictionary Python Dictionary values() Returns view of all values in dictionary 7:
print(d.get('key')) #None print(d.get('clear')) #0 print(d.get('copy')) #1Chúng tôi sử dụng điều đó để có được thông tin nếu khóa có mặt hoặc không. Nhưng hãy xem xét điều này nếu chúng ta tạo ra một dict với một Python Dictionary clear() Removes all Items Python Dictionary copy() Returns Shallow Copy of a Dictionary Python Dictionary fromkeys() Creates dictionary from given sequence Python Dictionary get() Returns Value of The Key Python Dictionary items() Returns view of dictionary (key, value) pair Python Dictionary keys() Returns View Object of All Keys Python Dictionary pop() Removes and returns element having given key Python Dictionary popitem() Returns & Removes Element From Dictionary Python Dictionary setdefault() Inserts Key With a Value if Key is not Present Python Dictionary update() Updates the Dictionary Python Dictionary values() Returns view of all values in dictionary 8:
d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #NoneDẫn đầu phương pháp Python Dictionary clear() Removes all Items Python Dictionary copy() Returns Shallow Copy of a Dictionary Python Dictionary fromkeys() Creates dictionary from given sequence Python Dictionary get() Returns Value of The Key Python Dictionary items() Returns view of dictionary (key, value) pair Python Dictionary keys() Returns View Object of All Keys Python Dictionary pop() Removes and returns element having given key Python Dictionary popitem() Returns & Removes Element From Dictionary Python Dictionary setdefault() Inserts Key With a Value if Key is not Present Python Dictionary update() Updates the Dictionary Python Dictionary values() Returns view of all values in dictionary 2 không đáng tin cậy trong trường hợp một số giá trị có thể là Python Dictionary clear() Removes all Items Python Dictionary copy() Returns Shallow Copy of a Dictionary Python Dictionary fromkeys() Creates dictionary from given sequence Python Dictionary get() Returns Value of The Key Python Dictionary items() Returns view of dictionary (key, value) pair Python Dictionary keys() Returns View Object of All Keys Python Dictionary pop() Removes and returns element having given key Python Dictionary popitem() Returns & Removes Element From Dictionary Python Dictionary setdefault() Inserts Key With a Value if Key is not Present Python Dictionary update() Updates the Dictionary Python Dictionary values() Returns view of all values in dictionary 7.
Câu chuyện này nên có một kết thúc hạnh phúc hơn. Nếu chúng ta sử dụng bộ so sánh d.get("key") 1:
print('key' in d) #True print('key2' in d) #FalseChúng tôi nhận được kết quả chính xác.
Chúng tôi có thể kiểm tra mã Byte Python:
import dis dis.dis("'key' in d") # 1 0 LOAD_CONST 0 ('key') # 2 LOAD_NAME 0 (d) # 4 COMPARE_OP 6 (in) # 6 RETURN_VALUE dis.dis("d.get('key2')") # 1 0 LOAD_NAME 0 (d) # 2 LOAD_METHOD 1 (get) # 4 LOAD_CONST 0 ('key2') # 6 CALL_METHOD 1 # 8 RETURN_VALUEĐiều này cho thấy d.get("key") 1 so sánh toán tử không chỉ đáng tin cậy hơn mà còn nhanh hơn Python Dictionary clear() Removes all Items Python Dictionary copy() Returns Shallow Copy of a Dictionary Python Dictionary fromkeys() Creates dictionary from given sequence Python Dictionary get() Returns Value of The Key Python Dictionary items() Returns view of dictionary (key, value) pair Python Dictionary keys() Returns View Object of All Keys Python Dictionary pop() Removes and returns element having given key Python Dictionary popitem() Returns & Removes Element From Dictionary Python Dictionary setdefault() Inserts Key With a Value if Key is not Present Python Dictionary update() Updates the Dictionary Python Dictionary values() Returns view of all values in dictionary 2.
Đưa ra một từ điển trong Python Nhiệm vụ của chúng tôi là kiểm tra xem khóa đã cho có có trong từ điển hay không. & nbsp; Nếu có mặt, in hiện tại hiện tại và giá trị của khóa. Mặt khác, in không có mặt hiện tại. & NBSP;
Thí dụ
Đầu vào: {‘A, {‘a’: 100, ‘b’:200, ‘c’:300}, key = b
Output : Present, value = 200Đầu vào: {‘X,: 25,’ y, 18, ‘z,: 45}, key = woutput: không có mặt {‘x’: 25, ‘y’:18, ‘z’:45}, key = w
Output : Not present
Có thể có nhiều cách khác nhau để kiểm tra xem khóa đã tồn tại, chúng tôi đã đề cập đến các phương pháp sau:
- Sử dụng các phím phương thức sẵn có () & nbsp;
- Sử dụng nếu và trong
- Sử dụng phương thức has_key ()
- Sử dụng phương thức get ()
Kiểm tra xem khóa có tồn tại bằng cách sử dụng các phím phương thức sẵn () & nbsp;
Sử dụng phương thức Phương thức Inbuilt () Phương thức trả về danh sách tất cả các khóa có sẵn trong từ điển. Với các phím phương thức sẵn có (), sử dụng câu lệnh IF với toán tử trong để kiểm tra xem khóa có có trong từ điển hay không. & NBSP;
Python3
d.get("key") 4 d.get("key") 5
d.get("key") 6d.get("key") 7 d.get("key") 8d.get("key") 1 d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 0
d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 1d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 2d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 3d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 4d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 5d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 6d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 7d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 8
d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 1d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 2d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 3print(d.get('key')) #None print(d.get('clear')) #0 print(d.get('copy')) #1 2print(d.get('key')) #None print(d.get('clear')) #0 print(d.get('copy')) #1 3
d.get("key") 6print(d.get('key')) #None print(d.get('clear')) #0 print(d.get('copy')) #1 5print(d.get('key')) #None print(d.get('clear')) #0 print(d.get('copy')) #1 6
d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 1d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 2d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 3d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 0d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 8
d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 2d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 6 d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 4d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 55556d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 7____58d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 9print(d.get('key')) #None print(d.get('clear')) #0 print(d.get('copy')) #1 6print('key' in d) #True print('key2' in d) #False 1d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 8print('key' in d) #True print('key2' in d) #False 3__
d.get("key") 8d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 6 d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 9
import dis dis.dis("'key' in d") # 1 0 LOAD_CONST 0 ('key') # 2 LOAD_NAME 0 (d) # 4 COMPARE_OP 6 (in) # 6 RETURN_VALUE dis.dis("d.get('key2')") # 1 0 LOAD_NAME 0 (d) # 2 LOAD_METHOD 1 (get) # 4 LOAD_CONST 0 ('key2') # 6 CALL_METHOD 1 # 8 RETURN_VALUE 0
d.get("key") 8d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 6 import dis dis.dis("'key' in d") # 1 0 LOAD_CONST 0 ('key') # 2 LOAD_NAME 0 (d) # 4 COMPARE_OP 6 (in) # 6 RETURN_VALUE dis.dis("d.get('key2')") # 1 0 LOAD_NAME 0 (d) # 2 LOAD_METHOD 1 (get) # 4 LOAD_CONST 0 ('key2') # 6 CALL_METHOD 1 # 8 RETURN_VALUE 3
import dis dis.dis("'key' in d") # 1 0 LOAD_CONST 0 ('key') # 2 LOAD_NAME 0 (d) # 4 COMPARE_OP 6 (in) # 6 RETURN_VALUE dis.dis("d.get('key2')") # 1 0 LOAD_NAME 0 (d) # 2 LOAD_METHOD 1 (get) # 4 LOAD_CONST 0 ('key2') # 6 CALL_METHOD 1 # 8 RETURN_VALUE 0
Đầu ra
Present, value = 200 Not presentKiểm tra xem khóa có tồn tại bằng cách sử dụng nếu và trong
Phương thức này sử dụng câu lệnh IF để kiểm tra xem khóa đã cho có tồn tại trong từ điển. & NBSP;
Python3
d.get("key") 4 d.get("key") 5
d.get("key") 6d.get("key") 7 d.get("key") 8d.get("key") 1 d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 0
d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 1d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 2d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 3d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 4d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 5d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 6d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 7d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 8
d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 1d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 2d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 3print(d.get('key')) #None print(d.get('clear')) #0 print(d.get('copy')) #1 2print(d.get('key')) #None print(d.get('clear')) #0 print(d.get('copy')) #1 3
d.get("key") 6print(d.get('key')) #None print(d.get('clear')) #0 print(d.get('copy')) #1 5print(d.get('key')) #None print(d.get('clear')) #0 print(d.get('copy')) #1 6
d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 1d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 2d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 3d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 0d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 8
d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 2d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 6 d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 4d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 55556d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 7____58d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 9print(d.get('key')) #None print(d.get('clear')) #0 print(d.get('copy')) #1 6print('key' in d) #True print('key2' in d) #False 1d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 8print('key' in d) #True print('key2' in d) #False 3__
d.get("key") 8d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 6 d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 9
import dis dis.dis("'key' in d") # 1 0 LOAD_CONST 0 ('key') # 2 LOAD_NAME 0 (d) # 4 COMPARE_OP 6 (in) # 6 RETURN_VALUE dis.dis("d.get('key2')") # 1 0 LOAD_NAME 0 (d) # 2 LOAD_METHOD 1 (get) # 4 LOAD_CONST 0 ('key2') # 6 CALL_METHOD 1 # 8 RETURN_VALUE 0
d.get("key") 8d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 6 import dis dis.dis("'key' in d") # 1 0 LOAD_CONST 0 ('key') # 2 LOAD_NAME 0 (d) # 4 COMPARE_OP 6 (in) # 6 RETURN_VALUE dis.dis("d.get('key2')") # 1 0 LOAD_NAME 0 (d) # 2 LOAD_METHOD 1 (get) # 4 LOAD_CONST 0 ('key2') # 6 CALL_METHOD 1 # 8 RETURN_VALUE 3
import dis dis.dis("'key' in d") # 1 0 LOAD_CONST 0 ('key') # 2 LOAD_NAME 0 (d) # 4 COMPARE_OP 6 (in) # 6 RETURN_VALUE dis.dis("d.get('key2')") # 1 0 LOAD_NAME 0 (d) # 2 LOAD_METHOD 1 (get) # 4 LOAD_CONST 0 ('key2') # 6 CALL_METHOD 1 # 8 RETURN_VALUE 0
Đầu ra
Present, value = 200 Not presentKiểm tra xem khóa có tồn tại bằng cách sử dụng nếu và trong
Phương thức này sử dụng câu lệnh IF để kiểm tra xem khóa đã cho có tồn tại trong từ điển. & NBSP;
Python3has_keys() method has been removed from the Python3 version. Therefore, it can be used in Python2 only.
d.get("key") 6d.get("key") 7 d.get("key") 8d.get("key") 1 Present, value = 200 Not present 1
d.get("key") 4 d.get("key") 5
d.get("key") 6d.get("key") 7 d.get("key") 8d.get("key") 1 d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 0
d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 1d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 2d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 3Python Dictionary clear() Removes all Items Python Dictionary copy() Returns Shallow Copy of a Dictionary Python Dictionary fromkeys() Creates dictionary from given sequence Python Dictionary get() Returns Value of The Key Python Dictionary items() Returns view of dictionary (key, value) pair Python Dictionary keys() Returns View Object of All Keys Python Dictionary pop() Removes and returns element having given key Python Dictionary popitem() Returns & Removes Element From Dictionary Python Dictionary setdefault() Inserts Key With a Value if Key is not Present Python Dictionary update() Updates the Dictionary Python Dictionary values() Returns view of all values in dictionary 34print(d.get('key')) #None print(d.get('clear')) #0 print(d.get('copy')) #1 3
d.get("key") 6print(d.get('key')) #None print(d.get('clear')) #0 print(d.get('copy')) #1 5print(d.get('key')) #None print(d.get('clear')) #0 print(d.get('copy')) #1 6
d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 1d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 2d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 3d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 0d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 8
d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 2d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 6 d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 4d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 55556d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 7____58d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 9print(d.get('key')) #None print(d.get('clear')) #0 print(d.get('copy')) #1 6print('key' in d) #True print('key2' in d) #False 1d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 8print('key' in d) #True print('key2' in d) #False 3__
d.get("key") 8d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 6 d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 9
import dis dis.dis("'key' in d") # 1 0 LOAD_CONST 0 ('key') # 2 LOAD_NAME 0 (d) # 4 COMPARE_OP 6 (in) # 6 RETURN_VALUE dis.dis("d.get('key2')") # 1 0 LOAD_NAME 0 (d) # 2 LOAD_METHOD 1 (get) # 4 LOAD_CONST 0 ('key2') # 6 CALL_METHOD 1 # 8 RETURN_VALUE 0
d.get("key") 8d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 6 import dis dis.dis("'key' in d") # 1 0 LOAD_CONST 0 ('key') # 2 LOAD_NAME 0 (d) # 4 COMPARE_OP 6 (in) # 6 RETURN_VALUE dis.dis("d.get('key2')") # 1 0 LOAD_NAME 0 (d) # 2 LOAD_METHOD 1 (get) # 4 LOAD_CONST 0 ('key2') # 6 CALL_METHOD 1 # 8 RETURN_VALUE 3
import dis dis.dis("'key' in d") # 1 0 LOAD_CONST 0 ('key') # 2 LOAD_NAME 0 (d) # 4 COMPARE_OP 6 (in) # 6 RETURN_VALUE dis.dis("d.get('key2')") # 1 0 LOAD_NAME 0 (d) # 2 LOAD_METHOD 1 (get) # 4 LOAD_CONST 0 ('key2') # 6 CALL_METHOD 1 # 8 RETURN_VALUE 0
Đầu ra
Python Dictionary clear() Removes all Items Python Dictionary copy() Returns Shallow Copy of a Dictionary Python Dictionary fromkeys() Creates dictionary from given sequence Python Dictionary get() Returns Value of The Key Python Dictionary items() Returns view of dictionary (key, value) pair Python Dictionary keys() Returns View Object of All Keys Python Dictionary pop() Removes and returns element having given key Python Dictionary popitem() Returns & Removes Element From Dictionary Python Dictionary setdefault() Inserts Key With a Value if Key is not Present Python Dictionary update() Updates the Dictionary Python Dictionary values() Returns view of all values in dictionary 0Kiểm tra xem khóa có tồn tại bằng cách sử dụng nếu và trong
Phương thức này sử dụng câu lệnh IF để kiểm tra xem khóa đã cho có tồn tại trong từ điển. & NBSP;
Python3
d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 2d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 6 d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 4d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 55556d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 7____58d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 9print(d.get('key')) #None print(d.get('clear')) #0 print(d.get('copy')) #1 6print('key' in d) #True print('key2' in d) #False 1d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 8print('key' in d) #True print('key2' in d) #False 3__
d.get("key") 8d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 6 d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 9
Python Dictionary clear() Removes all Items Python Dictionary copy() Returns Shallow Copy of a Dictionary Python Dictionary fromkeys() Creates dictionary from given sequence Python Dictionary get() Returns Value of The Key Python Dictionary items() Returns view of dictionary (key, value) pair Python Dictionary keys() Returns View Object of All Keys Python Dictionary pop() Removes and returns element having given key Python Dictionary popitem() Returns & Removes Element From Dictionary Python Dictionary setdefault() Inserts Key With a Value if Key is not Present Python Dictionary update() Updates the Dictionary Python Dictionary values() Returns view of all values in dictionary 90d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 2d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 3Python Dictionary clear() Removes all Items Python Dictionary copy() Returns Shallow Copy of a Dictionary Python Dictionary fromkeys() Creates dictionary from given sequence Python Dictionary get() Returns Value of The Key Python Dictionary items() Returns view of dictionary (key, value) pair Python Dictionary keys() Returns View Object of All Keys Python Dictionary pop() Removes and returns element having given key Python Dictionary popitem() Returns & Removes Element From Dictionary Python Dictionary setdefault() Inserts Key With a Value if Key is not Present Python Dictionary update() Updates the Dictionary Python Dictionary values() Returns view of all values in dictionary 93d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 8
print(d.get('key')) #None print(d.get('clear')) #0 print(d.get('copy')) #1 5print(d.get('key')) #None print(d.get('clear')) #0 print(d.get('copy')) #1 6
Python Dictionary clear() Removes all Items Python Dictionary copy() Returns Shallow Copy of a Dictionary Python Dictionary fromkeys() Creates dictionary from given sequence Python Dictionary get() Returns Value of The Key Python Dictionary items() Returns view of dictionary (key, value) pair Python Dictionary keys() Returns View Object of All Keys Python Dictionary pop() Removes and returns element having given key Python Dictionary popitem() Returns & Removes Element From Dictionary Python Dictionary setdefault() Inserts Key With a Value if Key is not Present Python Dictionary update() Updates the Dictionary Python Dictionary values() Returns view of all values in dictionary 90d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 2d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 3d.get("key") 00d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 8
Xử lý ngoại lệ keyerror
Sử dụng hãy thử và ngoại trừ để xử lý ngoại lệ KeyError để xác định xem có phải khóa trong một dict hay không. Ngoại lệ KeyError được tạo nếu khóa mà bạn đang cố gắng truy cập không có trong từ điển.
Python3
d.get("key") 02___
d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 2d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 3d.get("key") 19d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 8
d.get("key") 21print(d.get('key')) #None print(d.get('clear')) #0 print(d.get('copy')) #1 6
d.get("key") 6d.get("key") 24d.get("key") 25d.get("key") 26
d.get("key") 6d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 2d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 3d.get("key") 30d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 8
d.get("key") 32 d.get("key") 33
d.get("key") 6d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 2d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 3d.get("key") 37d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 8
d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 2d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 3d.get("key") 41d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 8
d.get("key") 21print(d.get('key')) #None print(d.get('clear')) #0 print(d.get('copy')) #1 6
d.get("key") 6d.get("key") 24d.get("key") 47d.get("key") 26
d.get("key") 6d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 2d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 3d.get("key") 30d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 8
d.get("key") 32 d.get("key") 33
d.get("key") 6d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 2d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 3d.get("key") 59d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 8
d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 2d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 3d.get("key") 41d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 8
Python Dictionary clear() Removes all Items Python Dictionary copy() Returns Shallow Copy of a Dictionary Python Dictionary fromkeys() Creates dictionary from given sequence Python Dictionary get() Returns Value of The Key Python Dictionary items() Returns view of dictionary (key, value) pair Python Dictionary keys() Returns View Object of All Keys Python Dictionary pop() Removes and returns element having given key Python Dictionary popitem() Returns & Removes Element From Dictionary Python Dictionary setdefault() Inserts Key With a Value if Key is not Present Python Dictionary update() Updates the Dictionary Python Dictionary values() Returns view of all values in dictionary 1Đầu ra
Sử dụng phương thức đếm ()
Python3
Phương thức đếm () có thể được sử dụng để kiểm tra xem khóa có tồn tại trong từ điển hay không, nếu số đếm của khóa là 1 thì khóa không có mặt khác.
d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 2___
d.get("key") 8d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 6 d= {'key':None} print(d.get('key')) #None print(d.get('key2')) #None 9
d.get("key") 79d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 6 d.get("key") 81d.get("key") 82
d.get("key") 83d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 6 Python Dictionary clear() Removes all Items Python Dictionary copy() Returns Shallow Copy of a Dictionary Python Dictionary fromkeys() Creates dictionary from given sequence Python Dictionary get() Returns Value of The Key Python Dictionary items() Returns view of dictionary (key, value) pair Python Dictionary keys() Returns View Object of All Keys Python Dictionary pop() Removes and returns element having given key Python Dictionary popitem() Returns & Removes Element From Dictionary Python Dictionary setdefault() Inserts Key With a Value if Key is not Present Python Dictionary update() Updates the Dictionary Python Dictionary values() Returns view of all values in dictionary 93
d.get("key") 7d.get("key") 87d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 6d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 6 d.get("key") 90d.get("key") 91
d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10} 2d.get("key") 97