Hướng dẫn what is a key in dictionary python - khóa trong từ điển python là gì

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 indexing 
0____11
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
2:
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
4
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
1
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
6______

2nd key using keys() :  for
0
2nd key using keys() :  for
1

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() :  for
2= {
2nd key using keys() :  for
5:
2nd key using keys() :  for
7
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
1____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 indexing 
1
2nd key using keys() :  for
5:
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'])
1Dictionary1 0

Dictionary1 1

2nd key using keys() :  for
0Dictionary1 3Dictionary1 4 Dictionary1 5 Dictionary1 6

keys()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'])
7Dictionary1 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() :  for
2= {
2nd key using keys() :  for
5:
2nd key using keys() :  for
7
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
1____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 indexing 
1
2nd key using keys() :  for
5:
Keys before Dictionary Updation:
dict_keys(['B', 'A'])

After dictionary is updated:
dict_keys(['B', 'A', 'C'])
5____19

2nd key using keys() :  for
0Dictionary1 3'A'0
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
1'A'2'A'3
Keys before Dictionary Updation:
dict_keys(['B', 'A'])

After dictionary is updated:
dict_keys(['B', 'A', 'C'])
1'A'5

Output:  

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 indexing 
0____11
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
2

2nd key using keys() :  for
0Dictionary1 3: 9
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
00

2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
01=
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
03

2nd key using keys() :  for
0
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
05

2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
06
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
6:
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
0
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
10

2nd key using keys() :  for
0Dictionary1 3
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
13
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
00

2nd key using keys() :  for
0
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
05

Output:  

Keys before Dictionary Updation:
dict_keys(['B', 'A'])

After dictionary is updated:
dict_keys(['B', 'A', 'C'])

Chìa khóa và giá trị trong python từ điển là gì?

Một mục có khóa và giá trị tương ứng được biểu thị dưới dạng cặp (khóa: giá trị). Mặc dù các giá trị có thể thuộc bất kỳ loại dữ liệu nào và có thể lặp lại, các khóa phải thuộc loại bất biến (chuỗi, số hoặc tuple với các yếu tố bất biến) và phải là duy nhất.. While the values can be of any data type and can repeat, keys must be of immutable type (string, number or tuple with immutable elements) and must be unique.

Chìa khóa trong chức năng Python là gì?

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.Cú pháp: Dict.Keys () Tham số: Không có tham số.Trả về: Một đối tượng xem được trả về hiển thị tất cả các khóa.returns a view object that displays a list of all the keys in the dictionary in order of insertion using Python. Syntax: dict.keys() Parameters: There are no parameters. Returns: A view object is returned that displays all the keys.

Điều gì có thể là một chìa khóa trong một từ điển?

Thứ hai, một khóa từ điển phải thuộc loại bất biến.Ví dụ: bạn có thể sử dụng số nguyên, phao, chuỗi hoặc boolean làm khóa từ điển.Tuy nhiên, cả danh sách và từ điển khác đều không thể đóng vai trò là khóa từ điển, bởi vì danh sách và từ điển đều có thể thay đổi.integer, float, string, or Boolean as a dictionary key. However, neither a list nor another dictionary can serve as a dictionary key, because lists and dictionaries are mutable.

Từ điển có Python chính không?

Từ điển là một thùng chứa tích hợp trong Python lưu trữ các cặp giá trị khóa, với các khóa được sử dụng làm chỉ mục.Ở đây, các khóa có thể là số hoặc chuỗi, nhưng chúng không thể là các chuỗi hoặc đối tượng có thể thay đổi như danh sách.. Here, keys can be numbers or strings, but they can't be mutable sequences or objects like lists.