Three methods for python+selenium dictionary to look up key by value

Original article: https://blog.csdn.net/ssswill/article/details/86618553 The
original article is very well written, and it helped me a lot. Because there is no forwarding function, I can’t go directly to my list, all directly copied Up

dictionary:

student = {
    
    '小萌': '1001', '小智': '1002', '小强': '1003', '小明': '1004'}

Method 1:
List the dictionary

list (student.keys()) [list (student.values()).index ('1004')]
'小明'

Method 2:

def get_key (dict, value):
    return [k for k, v in dict.items() if v == value]
get_key (student, '1002')

Method 3:
Exchange the key and val of the dictionary.

new_dict = {
    
    v : k for k, v in student.items()}
new_dict ['1001']
'小萌'

Guess you like

Origin blog.csdn.net/zhaoweiya/article/details/107100597