Finding key from value in Python dictionary:

前端 未结 4 2007
误落风尘
误落风尘 2020-12-06 10:53

Fairly new to Python, still struggling with so much information.

All the documentation I\'ve seen about dictionaries explain various ways of getting a value via a k

4条回答
  •  醉酒成梦
    2020-12-06 11:19

    Since your dictionary can contain duplicate values (i.e. {'a': 'A', 'b': 'A'}), the only way to find a key from value is to iterate over the dictionary as you describe.

    Or... build the opposite dictionary. you have to recreate it after each modification of the original dictionary.

    Or... write a class that maintains both-ways dictionary. You will have to manage situations where duplicate value appears.

提交回复
热议问题