Hướng dẫn how do you print a dictionary key in python? - làm thế nào để bạn in một khóa từ điển trong python?

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;

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

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 (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
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 (biểu thị mã enzyme EC).keys are of the form
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 (representing source and target nodes, and the edge type), with values of the form
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 (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ế

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

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

5.

Hướng dẫn how do you print a dictionary key in python? - làm thế nào để bạn in một khóa từ điển trong python?

Các mục từ điển Python được trình bày trong khóa: các cặp giá trị. Chúng được đặt hàng, có thể sửa đổi và không cho phép trùng lặp. Để tìm bao nhiêu mục mà một từ điển có, hãy sử dụng hàm Len ().

Để in DictionaryKeys trong Python, hãy sử dụng & nbsp; dict.keys () & nbsp; Phương thức với hàm in () để in các khóa đó. Phương thức Dict.Keys () 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.print the dictionary keys in Python, use the dict.keys() method with the print() function to print those keys. The dict.keys() method returns a view object that displays a list of all the keys in the dictionary.

Phương thức Dict.Keys () có thể được sử dụng để truy cập các phần tử từ điển, như chúng ta có thể làm cho một danh sách theo chỉ mục. Hãy để xác định một từ điển và sau đó sử dụng phương thức Dict.Keys () và Print () để in các phím.dict.keys() method can be used to access the dictionary elements, as we can do for a list by index. Let’s define a dictionary and then use the dict.keys() and print() method to print the keys.

dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

print(dict.keys())

Đầu ra

dict_keys(['streaming', 'show', 'ratings', 'genre'])

Và bạn nhận được danh sách các khóa từ điển.

Sử dụng phương thức từ điển.items ()

Các mục từ điển () là một phương thức Python tích hợp được sử dụng để có được tất cả các khóa và giá trị liên quan với các khóa đó. Chúng ta có thể sử dụng phương thức Item () với vòng lặp và in các khóa và giá trị của từ điển.

Cách tiếp cận này hữu ích hơn nếu bạn muốn in một khóa mỗi lần.

dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

for key, value in dict.items():
    print(key)

Đầu ra

streaming
show
ratings
genre

Và bạn nhận được danh sách các khóa từ điển.dict.items() with print() method.

Sử dụng phương thức từ điển.items ()dict.keys() with print() method.

Các mục từ điển () là một phương thức Python tích hợp được sử dụng để có được tất cả các khóa và giá trị liên quan với các khóa đó. Chúng ta có thể sử dụng phương thức Item () với vòng lặp và in các khóa và giá trị của từ điển.dict.values() with print() method.

Cách tiếp cận này hữu ích hơn nếu bạn muốn in một khóa mỗi lần.

Để in các khóa và giá trị của từ điển, hãy sử dụng phương thức Dict.items () bằng phương thức in ().

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)

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
dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

print(dict.keys())
0
dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

print(dict.keys())
1
dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

print(dict.keys())
2223
dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

print(dict.keys())
0
dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

print(dict.keys())
5
dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

print(dict.keys())
2227________________
dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

print(dict.keys())
1
dict_keys(['streaming', 'show', 'ratings', 'genre'])
0

dict_keys(['streaming', 'show', 'ratings', 'genre'])
1
dict_keys(['streaming', 'show', 'ratings', 'genre'])
2

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

Các

dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

for key, value in dict.items():
    print(key)
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)

7
streaming
show
ratings
genre
0

streaming
show
ratings
genre
1
streaming
show
ratings
genre
2
streaming
show
ratings
genre
3
streaming
show
ratings
genre
4

streaming
show
ratings
genre
5
streaming
show
ratings
genre
6
streaming
show
ratings
genre
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)

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)

7
dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

for key, value in dict.items():
    print(key)
2
dict_keys(['A', 'B', 'C'])
1

dict_keys(['A', 'B', 'C'])
2
dict_keys(['streaming', 'show', 'ratings', 'genre'])
1
dict_keys(['A', 'B', 'C'])
4
dict_keys(['A', 'B', 'C'])
5
dict_keys(['A', 'B', 'C'])
6
dict_keys(['A', 'B', 'C'])
7

streaming
show
ratings
genre
5
dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

for key, value in dict.items():
    print(key)
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)

7
dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

for key, value in dict.items():
    print(key)
8
dict_keys(['A', 'B', 'C'])
6
dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

for key, value in dict.items():
    print(key)
2

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

Các

dict_keys(['streaming', 'show', 'ratings', 'genre'])
1
dict_keys(['A', 'B', 'C'])
4
Keys before Dictionary Updation:
dict_keys(['B', 'A'])

After dictionary is updated:
dict_keys(['B', 'A', 'C'])
1
dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

print(dict.keys())
2
Keys before Dictionary Updation:
dict_keys(['B', 'A'])

After dictionary is updated:
dict_keys(['B', 'A', 'C'])
3
Keys before Dictionary Updation:
dict_keys(['B', 'A'])

After dictionary is updated:
dict_keys(['B', 'A', 'C'])
4
dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

for key, value in dict.items():
    print(key)
2
Keys before Dictionary Updation:
dict_keys(['B', 'A'])

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

Output:  

dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

for key, value in dict.items():
    print(key)
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)

7
streaming
show
ratings
genre
0

streaming show ratings genre1 streaming show ratings genre2streaming show ratings genre3 streaming show ratings genre4

streaming
show
ratings
genre
5
streaming
show
ratings
genre
6
streaming
show
ratings
genre
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)

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)

7
dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

for key, value in dict.items():
    print(key)
2
dict_keys(['A', 'B', 'C'])
1

Python3

dict_keys(['A', 'B', 'C'])
2
dict_keys(['streaming', 'show', 'ratings', 'genre'])
1
dict_keys(['A', 'B', 'C'])
4
dict_keys(['A', 'B', 'C'])
5
dict_keys(['A', 'B', 'C'])
6
dict_keys(['A', 'B', 'C'])
7

streaming
show
ratings
genre
5
dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

for key, value in dict.items():
    print(key)
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)

7
dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

for key, value in dict.items():
    print(key)
8
dict_keys(['A', 'B', 'C'])
6
dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

for key, value in dict.items():
    print(key)
2

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;

dict_keys(['streaming', 'show', 'ratings', 'genre'])
1
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)

16

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 = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

print(dict.keys())
7
dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

print(dict.keys())
0
dict = {
    'streaming': 'Netflix',
    'show': 'Shadow and Bone',
    'ratings': 'Good',
    'genre': 'Fantasy '
}

print(dict.keys())
1
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)

21

dict_keys(['streaming', 'show', 'ratings', 'genre'])
1
dict_keys(['A', 'B', 'C'])
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)

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

11

dict_keys(['streaming', 'show', 'ratings', 'genre'])
1
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)

16

Output:  

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

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

Bạn có thể in từ điển bằng Python không?

Bạn có thể in một từ điển bằng Python bằng cách sử dụng cho các vòng lặp hoặc mô -đun JSON.Cách tiếp cận vòng lặp là tốt nhất nếu bạn muốn hiển thị nội dung của từ điển với bảng điều khiển trong khi phương pháp mô -đun JSON phù hợp hơn với các trường hợp sử dụng nhà phát triển.. The for loop approach is best if you want to display the contents of a dictionary to a console whereas the json module approach is more appropriate for developer use cases.

Phương pháp để có được chìa khóa trong từ điển trong Python là gì?

Phương thức từ điển Python Phương thức Phương thức Phương thức () Phương thức trả về đối tượng Chế độ xem.Đối tượng xem chứa các khóa của từ điển, như một danh sách.keys() Method The keys() method returns a view object. The view object contains the keys of the dictionary, as a list.