#定义一个新的类,继承dict类,实现根据value或者key的功能
class ValueDict(dict):
    #定义构造函数
    def __init__(self, *args, **kargs):
        #调用父类的初始化函数
        super().__init__(*args,**kargs)

    #add new function named getkeys()
    def getkeys(self, val):
        result = []
        # result.append(value) for key , value in self.items() if value = val
        for key, value in self.items():
            if value == val : result.append(key)
        return result

my_dict = ValueDict(chinese=80,math=80,english=60)
print(my_dict.getkeys(80))

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-31
  • 2022-02-16
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-04
  • 2021-11-09
  • 2021-06-18
  • 2021-12-04
  • 2021-05-27
  • 2022-02-10
相关资源
相似解决方案