Phương pháp từ điển
Thí dụ
Trả lại các phím:
xe = {& nbsp; "Thương hiệu": "Ford", & NBSP; "Mô hình": "Mustang", & nbsp; "Năm": 1964}
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.keys []
print[x]
Hãy tự mình thử »
Định nghĩa và cách sử dụng
Phương thức keys[]
trả về một đối tượng xem. Đối tượng xem chứa các khóa của từ điển, như một danh sách.
Đối tượng xem sẽ phản ánh bất kỳ thay đổi nào được thực hiện cho từ điển, xem ví dụ bên dưới.
Cú pháp
Giá trị tham số
Không có tham số
Nhiều ví dụ hơn
Thí dụ
Khi một mục được thêm vào từ điển, đối tượng xem cũng được cập nhật:
xe = {& nbsp; "Thương hiệu": "Ford", & NBSP; "Mô hình": "Mustang", & nbsp; "Năm": 1964}
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.keys []
Hãy tự mình thử »
Định nghĩa và cách sử dụng
Hãy tự mình thử »
Phương pháp từ điển
Phương thức khóa [] trong từ điển Python, trả về một đối tượng xem hiển thị danh sách tất cả các khóa trong từ điển theo thứ tự chèn bằng Python.keys[] method in Python Dictionary, returns a view object that displays a list of all the keys in the dictionary in order of insertion using Python.
Cú pháp: dict.Keys [] dict.keys[]
Tham số: Không có tham số. There are no parameters.
Trả về: Một đối tượng xem được trả về hiển thị tất cả các khóa. Quan điểm này đối tượng thay đổi theo những thay đổi trong từ điển. A view object is returned that displays all the keys. This view object changes according to the changes in the dictionary.
Phương pháp 1: Truy cập khóa bằng hàm SEONs [] Accessing the key using the keys[] function
Một ví dụ đơn giản để hiển thị cách hoạt động của hàm khóa [] trong từ điển.
Python3
Dictionary1
=
{
'A'
:
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing0____11
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing2
:
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing4
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing1
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing6______
2nd key using keys[] : for0
2nd key using keys[] : for1
Output:
dict_keys[['A', 'B', 'C']]
Phương thức & NBSP; 2: Từ điển truy cập Python bằng khóa
Thể hiện ứng dụng thực tế của các khóa [] bằng cách sử dụng vòng python.
Python3
2nd key using keys[] : for2
=
{
2nd key using keys[] : for5
:
2nd key using keys[] : for7
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing1____29
:
Keys before Dictionary Updation: dict_keys[['B', 'A']] After dictionary is updated: dict_keys[['B', 'A', 'C']]1
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing1
2nd key using keys[] : for5
:
Keys before Dictionary Updation: dict_keys[['B', 'A']] After dictionary is updated: dict_keys[['B', 'A', 'C']]5____19
Keys before Dictionary Updation: dict_keys[['B', 'A']] After dictionary is updated: dict_keys[['B', 'A', 'C']]7
=
Keys before Dictionary Updation: dict_keys[['B', 'A']] After dictionary is updated: dict_keys[['B', 'A', 'C']]9
keys[]
0 keys[]
1keys[]
2 keys[]
3
keys[]
4keys[]
5 keys[]
6=
=
Keys before Dictionary Updation: dict_keys[['B', 'A']] After dictionary is updated: dict_keys[['B', 'A', 'C']]1
Dictionary1
0Dictionary1
1
2nd key using keys[] : for0
Dictionary1
3Dictionary1
4 Dictionary1
5 Dictionary1
6keys[]
4
Keys before Dictionary Updation: dict_keys[['B', 'A']] After dictionary is updated: dict_keys[['B', 'A', 'C']]7
=
Keys before Dictionary Updation: dict_keys[['B', 'A']] After dictionary is updated: dict_keys[['B', 'A', 'C']]7
Dictionary1
5 Keys before Dictionary Updation: dict_keys[['B', 'A']] After dictionary is updated: dict_keys[['B', 'A', 'C']]1
Output:
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing
Lưu ý: Cách tiếp cận thứ hai sẽ không hoạt động vì Dict_Keys trong Python 3 không hỗ trợ lập chỉ mục. & NBSP;The second approach would not work because dict_keys in Python 3 do not support indexing.
Phương thức & NBSP; 3: Truy cập khóa bằng cách sử dụng SEONS [] lập chỉ mục
Ở đây, trước tiên chúng tôi trích xuất tất cả các khóa và sau đó chúng tôi đã ngầm chuyển đổi chúng thành danh sách Python để truy cập phần tử từ nó.
Python3
2nd key using keys[] : for2
=
{
2nd key using keys[] : for5
:
2nd key using keys[] : for7
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing1____29
:
Keys before Dictionary Updation: dict_keys[['B', 'A']] After dictionary is updated: dict_keys[['B', 'A', 'C']]1
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing1
2nd key using keys[] : for5
:
Keys before Dictionary Updation: dict_keys[['B', 'A']] After dictionary is updated: dict_keys[['B', 'A', 'C']]5____19
2nd key using keys[] : for0
Dictionary1
3'A'
02nd key using loop : for TypeError: 'dict_keys' object does not support indexing1
'A'
2'A'
3Keys before Dictionary Updation: dict_keys[['B', 'A']] After dictionary is updated: dict_keys[['B', 'A', 'C']]1
'A'
5Output:
2nd key using keys[] : for
Phương thức & NBSP; 4: Hàm Cập nhật Dictionary [] của Python
Để chỉ ra cách cập nhật các khóa từ điển bằng hàm Update []. Ở đây, khi từ điển được cập nhật, các khóa cũng tự động được cập nhật để hiển thị các thay đổi.
Python3
Dictionary1
=
{
'A'
:
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing0____11
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing2
2nd key using keys[] : for0
Dictionary1
3:
92nd key using loop : for TypeError: 'dict_keys' object does not support indexing00
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing01
=
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing03
2nd key using keys[] : for0
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing05
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing06
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing6
:
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing0
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing10
2nd key using keys[] : for0
Dictionary1
32nd key using loop : for TypeError: 'dict_keys' object does not support indexing13
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing00
2nd key using keys[] : for0
2nd key using loop : for TypeError: 'dict_keys' object does not support indexing05
Output:
Keys before Dictionary Updation: dict_keys[['B', 'A']] After dictionary is updated: dict_keys[['B', 'A', 'C']]