Hướng dẫn how do i print a dictionary key in python 3? - làm cách nào để in khóa từ điển trong python 3?

Tôi đang thêm câu trả lời này là một trong những câu trả lời khác ở đây (https://stackoverflow.com/a/5905752/1904943) là ngày (Python 2; iteritems) và mã được trình bày - nếu được cập nhật cho Python 3 mỗi Giải pháp giải quyết được đề xuất trong một nhận xét cho câu trả lời đó - âm thầm không trả lại tất cả các dữ liệu liên quan.


Tiểu sử

Tôi có một số dữ liệu trao đổi chất, được biểu thị trong một biểu đồ (các nút, cạnh, ...). Trong biểu diễn từ điển của các dữ liệu đó, các khóa có dạng (604, 1037, 0) (biểu thị các nút nguồn và mục tiêu và loại cạnh), với các giá trị của dạng 5.3.1.9 (biểu thị mã enzyme EC).keys are of the form (604, 1037, 0) (representing source and target nodes, and the edge type), with values of the form 5.3.1.9 (representing EC enzyme codes).

Tìm các khóa cho các giá trị đã cho

Mã sau tìm thấy chính xác các khóa của tôi, các giá trị đã cho:

def k4v_edited(my_dict, value):
    values_list = []
    for k, v in my_dict.items():
        if v == value:
            values_list.append(k)
    return values_list

print(k4v_edited(edge_attributes, '5.3.1.9'))
## [(604, 1037, 0), (604, 3936, 0), (1037, 3936, 0)]

Trong khi mã này chỉ trả về các khóa đầu tiên (có thể là một số phù hợp):

def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

Mã thứ hai, được cập nhật ngây thơ thay thế iteritems bằng

def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

0, không trả lại
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

1.

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

def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

2
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

3
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

4
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

5
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

6
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

7
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

8
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

9
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

6
dict_keys(['A', 'B', 'C'])
1__18__

dict_keys(['A', 'B', 'C'])
7
dict_keys(['A', 'B', 'C'])
8

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

dict_keys(['A', 'B', 'C'])
9
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

3
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

4
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
2
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

6
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
4
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

8
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
6
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

6
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
8__

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.

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

After dictionary is updated:
dict_keys(['B', 'A', 'C'])
8
dict_keys(['A', 'B', 'C'])
7iteritems0iteritems1 iteritems2 iteritems3

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

After dictionary is updated:
dict_keys(['B', 'A', 'C'])
1
2nd key using keys() :  for
4
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

3
2nd key using keys() :  for
4iteritems2
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
8

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

dict_keys(['A', 'B', 'C'])
9
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

3
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

4
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
2
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

6
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
4
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

8
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
6
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

6
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
8__

dict_keys(['A', 'B', 'C'])
7iteritems05.3.1.97
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

85.3.1.99iteritems0
2nd key using loop : for
TypeError: 'dict_keys' object does not support indexing 
8iteritems2

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

def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

2
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

3
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

4
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

5
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

6
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

7
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

8
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

9
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

6
dict_keys(['A', 'B', 'C'])
1
dict_keys(['A', 'B', 'C'])
6

dict_keys(['A', 'B', 'C'])
7iteritems0
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

06
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

07

def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

08
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

3
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

10

dict_keys(['A', 'B', 'C'])
7
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

12

def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

13
dict_keys(['A', 'B', 'C'])
3
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

6
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

7
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

17

dict_keys(['A', 'B', 'C'])
7iteritems0
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

20
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

07

dict_keys(['A', 'B', 'C'])
7
def k4v(my_dict, value):
    for k, v in my_dict.items():
        if v == value:
            return k

print(k4v(edge_attributes, '5.3.1.9'))
## (604, 1037, 0)

12

Output:  

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

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

Làm thế nào để bạn in một khóa

Để in các cặp giá trị khóa cụ thể của từ điển:..
Sử dụng dict. Phương thức () phương thức để có được cái nhìn của các mục từ điển ..
Sử dụng một vòng lặp để lặp qua chế độ xem ..
Kiểm tra xem mỗi giá trị có đáp ứng một điều kiện không ..
Sử dụng hàm in () để in các cặp giá trị khóa phù hợp ..

Làm thế nào để bạn tìm nạp và chỉ hiển thị các khóa của một từ điển trong Python?

Phương thức từ điển Python Phương thức Phương thức Phương thức () Phương thức trong Từ điển Python, trả về một đối tượng Chế độ 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. 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. The 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. Parameters: There are no parameters. Returns: A view object is returned that displays all the keys.

Làm thế nào để bạn có được một khóa và giá trị từ điển?

Bạn có thể sử dụng phương thức get () của từ điển (dict) để nhận bất kỳ giá trị mặc định nào mà không có lỗi nếu khóa không tồn tại.Chỉ định khóa là đối số đầu tiên.Giá trị tương ứng được trả về nếu khóa tồn tại và không có giá trị nào được trả về nếu khóa không tồn tại.use the get() method of the dictionary ( dict ) to get any default value without an error if the key does not exist. Specify the key as the first argument. The corresponding value is returned if the key exists, and None is returned if the key does not exist.

Làm thế nào để bạn có được một chìa khóa từ một danh sách từ điển?

Sử dụng hàm () () và áp dụng nó vào từ điển đầu vào để lấy danh sách tất cả các khóa của từ điển.In danh sách các khóa của từ điển bằng cách đi qua từng khóa trong danh sách các khóa trên bằng cách sử dụng danh sách hiểu và cho vòng lặp.. Print the list of keys of a dictionary by traversing through each key in the above keys list using the list comprehension and for loop.