python,字典中如何根据value值取对应的key值
本文介绍了python,字典中如何根据value值取对应的key值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
比如:
dicxx = {'a':'001', 'b':'002'}
需要根据输入001,得到a
如果操作?百度了半天,没找到答案。感谢。
解决方案
帮你搜索了一下、
>>> dicxx = {'a':'001', 'b':'002'}
>>> list(dicxx.keys())[list(dicxx.values()).index("001")]
'a'
>>>
引用一段Python3文档里面的原话。
If keys, values and items views are iterated over with no intervening modifications to the dictionary, the order of items will directly correspond.
也就是说,在你迭代的过程中如果没有发生对字典的修改,那么.keys() and .values 这两个函数返回的 dict-view对象总是保持对应关系。
这篇关于python,字典中如何根据value值取对应的key值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文