Get key by value in dictionary

后端 未结 30 2392
北恋
北恋 2020-11-21 06:28

I made a function which will look up ages in a Dictionary and show the matching name:

dictionary = {\'george\' : 16, \'amber\' : 19}
search_age          


        
30条回答
  •  遥遥无期
    2020-11-21 07:17

    There is none. dict is not intended to be used this way.

    dictionary = {'george': 16, 'amber': 19}
    search_age = input("Provide age")
    for name, age in dictionary.items():  # for name, age in dictionary.iteritems():  (for Python 2.x)
        if age == search_age:
            print(name)
    

提交回复
热议问题