本文介绍了如何在Python字典中打印给定值的键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,假设我们有以下字典:
For example lets say we have the following dictionary:
dictionary = {'A':4,
'B':6,
'C':-2,
'D':-8}
如何给出给定值的特定键?
How can you print a certain key given its value?
print(dictionary.get('A')) #This will print 4
如何向后做呢?即不是通过引用键来获取值,而是通过引用值来获取键.
How can you do it backwards? i.e. instead of getting a value by referencing the key, getting a key by referencing the value.
推荐答案
我不认为有办法.这不是打算使用字典的方式...相反,您必须执行与此类似的操作.
I don't believe there is a way to do it. It's not how a dictionary is intended to be used...Instead, you'll have to do something similar to this.
for key, value in dictionary.items():
if 4 == value:
print key
这篇关于如何在Python字典中打印给定值的键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!