Python 根据字典的Value获取对应的Key
本文最后更新于 2022年5月17日 下午
Python的字典是非常好用的,但是如何在已知Value的情况获得对应的Key呢?
- 一个字典的Key是不一样的
- 一个字典的Vaulev是可能重复的
根据Python官方的描述:
If keys, values and items views are iterated over with no intervening modifications to the dictionary, the order of items will directly correspond.
在迭代的过程中,没有对字典进行修改,那么Key和Value总是保持对应关系。
列表解析式
1 | |
利用 keys() 、values()、index() 函数
1 | |
将原字典进行反转得新字典(此方法仅限于Value没有重复值)
1 | |
Python 根据字典的Value获取对应的Key
https://yuluod.github.io/2022/05/17/Python-根据字典的Value获取对应的Key/