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 [//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. 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]
6def 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]
8def 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]
9dict = { '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 genre0
streaming show ratings genre1
streaming show ratings genre2
streaming show ratings genre3
streaming show ratings genre4
streaming show ratings genre5
streaming show ratings genre6
streaming show ratings genre7
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]
7def 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 genre5
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 genre0
streaming
show
ratings
genre
1 streaming
show
ratings
genre
2streaming
show
ratings
genre
3 streaming
show
ratings
genre
4
streaming show ratings genre5
streaming show ratings genre6
streaming show ratings genre7
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]
7def 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 genre5
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]
16def 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]
17dict = { '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]
21dict_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]
24def 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]
11dict_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]
16Output:
Keys before Dictionary Updation: dict_keys[['B', 'A']] After dictionary is updated: dict_keys[['B', 'A', 'C']]