def __repr__(self): return "Enemy(%d,%s,%d,%d,%d)"%(self.id,self.name,self.atk,self.hp,self.atk_speed) L = [ Enemy(100,"张三",10,50,5), Enemy(101,"李四",15,20,7), Enemy(102,"王五",5,0,2), Enemy(103,"莉莉",20,0,13), Enemy(104,"芳芳",17,30,9) ] # 解决的问题1: # 获取敌人列表中,攻击力最小的敌人. # 使用内置高阶函数和ListHelper实现. result = ListHelper.get_min(L,lambda e : e.atk) print(result) result = min(L,key = lambda e : e.atk) print(result) # 解决的问题2: # 根据血量对敌人列表进行降叙排列 # 使用内置高阶函数和ListHelper实现. for item in sorted(L,key = lambda e : e.hp,reverse=True): print(item) ListHelper.order_by_descending(L,lambda e : e.hp) print(L) ListHelper.sort(L,lambda e : e.hp,reverse=True) print(L)
# # ascend = ListHelper.ascend(list_enemy, lambda e: e.atk) # for item in ascend: # print(item.atk, item.name) # # descend = ListHelper.descend(list_enemy, lambda e:e.atk) # for item in ascend: # print(item.atk, item.name) # min_atk = ListHelper.get_min(list_enemy, lambda e: e.atk) # print(min_atk.name, min_atk.atk) # max_atk = ListHelper.get_max(list_enemy, lambda e: e.atk) # print(max_atk.name, max_atk.atk) # ascend_atk = sorted(list_enemy, key=lambda e: e.atk, reverse=False) # for item in ascend_atk: # print(item.name, item.atk) # # descend_atk = sorted(list_enemy, key=lambda e: e.atk, reverse=True) # for item in descend_atk: # print(item.name, item.atk) # # ascend_atk = ListHelper.sort(list_enemy, lambda a, b: a.atk > b.atk) # for item in ascend_atk: # print(item.name, item.atk) # descend_atk = ListHelper.sort(list_enemy, lambda a, b: a.atk < b.atk) for item in descend_atk: print(item.name, item.atk)