Python中的反向字典查找 [英] Inverse dictionary lookup in Python

查看:34
本文介绍了Python中的反向字典查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么直接的方法可以通过知道字典中的值来找到一个键?

Is there any straightforward way of finding a key by knowing the value within a dictionary?

我能想到的是:

key = [key for key, value in dict_obj.items() if value == 'value'][0]

推荐答案

没有.不要忘记,可以在任意数量的键上找到该值,包括 0 或大于 1.

There is none. Don't forget that the value may be found on any number of keys, including 0 or more than 1.

这篇关于Python中的反向字典查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆