Get key by value in dictionary

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

    Sometimes int() may be needed:

    titleDic = {'Фильмы':1, 'Музыка':2}
    
    def categoryTitleForNumber(self, num):
        search_title = ''
        for title, titleNum in self.titleDic.items():
            if int(titleNum) == int(num):
                search_title = title
        return search_title
    

提交回复
热议问题