Get key by value in dictionary

后端 未结 30 2336
北恋
北恋 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:11

    Here, recover_key takes dictionary and value to find in dictionary. We then loop over the keys in dictionary and make a comparison with that of value and return that particular key.

    def recover_key(dicty,value):
        for a_key in dicty.keys():
            if (dicty[a_key] == value):
                return a_key
    

提交回复
热议问题