def __str__(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. 查找所有死人. for item in ListHelper.find_all(L, lambda enemy: enemy.hp == 0): print(item) print() # 2. 查找编号是101的敌人 target = ListHelper.first(L, lambda enemy: enemy.id == 101) print(target) print() # 3. 查找所有活人. for item in ListHelper.find_all(L, lambda enemy: enemy.hp > 0): print(item) print() # 4. 计算所有敌人攻击力总和 result = ListHelper.sum(L, lambda enemy: enemy.atk)
# print(enemy_last_alive.name) # enemy_last_atk = ListHelper.last_match(list_enemy, lambda item:item.atk_speed > 3) # print(enemy_last_atk.name) # """ 解决的问题:获取满条件的对象总数 获取生命值大于0的敌人总数 获取攻击速度小于20的敌人总数 """ # sum_alive = ListHelper.sum(list_enemy, lambda item: item.hp > 0) # sum_atk_speed = ListHelper.sum(list_enemy, lambda item: item.atk_speed > 3) # print(sum_alive) # print(sum_atk_speed) """ 解决的问题:判断列表中是否包含某个元素 判断列表中是否存在生命值为0的敌人 判断列表中是否存在攻击速度小于20的敌人 """ # hp_equal_0 = ListHelper.exist(list_enemy, lambda item:item.hp == 0) # atk_speed_greater_x = ListHelper.exist(list_enemy, lambda item:item.atk_speed > 3) # print(hp_equal_0) # print(atk_speed_greater_x) """ 删除hp等于0的敌人 删除编号是101的敌人 删除攻击力小于5的敌人 """ list02 = [5, 7, 9, 4, 2, 3, 5, 6] ListHelper.delete(list02, lambda item: item == 5) print(list02)
self.cd = cd self.atk = atk self.costSP = costSP list_skills = [ SkillData(101, "降龙十八掌", 60, 10, 5), SkillData(102, "如来神掌", 50, 15, 0), SkillData(103, "六脉神剑", 0, 20, 8), SkillData(104, "一阳指", 0, 50, 15), SkillData(105, "冷酷追击", 15, 30, 9), ] # 作业1: # 解决的问题1: # 获取敌人列表中,攻击力最小的敌人. result = ListHelper.get_min(list_skills, lambda e: e.atk) print(result.name) result = min(list_skills, key=lambda e: e.atk) print(result.name) # 解决的问题2: # 根据cd对技能列表进行降叙排列 # 使用内置高阶函数和ListHelper实现. # ListHelper.order_by_descending(list_skills,lambda e:e.cd) # for item in list_skills: # print(item.cd) # for item in sorted(list_skills,key = lambda e:e.cd,reverse=True): # print(item.cd)
] # 1. filter 过滤,类似于ListHelper.find_all. # 过滤出编号大于102的敌人 # for item in filter(lambda e: e.id > 102, list01): # print(item.id) # # for item in ListHelper.find_all(list01, lambda e: e.id > 102): # print(item.id) # 2. map 映射,类似于ListHelper.select # 映射出所有敌人的姓名 for item in map(lambda e: e.name, list01): print(item) for item in ListHelper.select(list01, lambda e: e.name): print(item) # # 按照血量升序排列,类似于ListHelper.order_by # for item in sorted(list01,key = lambda e:e.hp ): # print(item.hp) # # # 按照血量降叙排列 # for item in sorted(list01,key = lambda e:e.hp,reverse=True): # print(item.hp) # ListHelper.order_by(list01,lambda e:e.hp) # for item in list01: # print(item.hp) # 获取攻击力最大的敌人
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) ] # 解决的问题:获取满足条件的最后一个对象 # 获取最后一个活人 # 获取攻击速度大于5的最后一个敌人 # result = ListHelper.first(L[::-1],lambda enemy : enemy.hp > 0) result = ListHelper.last(L, lambda enemy: enemy.hp > 0) print(result) result = ListHelper.first(L[::-1], lambda enemy: enemy.atk_speed > 5) print(result) # 解决的问题:获取满足条件的对象总数 # 获取具有生命值的对象总数 # 获取攻击速度小于20的敌人总数 result = ListHelper.count(L, lambda enemy: enemy.hp > 0) print(result) result = ListHelper.count(L, lambda enemy: enemy.atk_speed < 20) print(result) # 解决的问题:判断列表中是否包含某个元素 # 获取列表中是否具有死人 # 获取列表中是否具有攻击速度大于10的敌人
self.name = name self.hp = hp self.atk = atk self.atk_speed = atk_speed list01 = [ Enemy(101, "玄冥大老", 200, 800, 5), Enemy(102, "玄冥小老", 150, 700, 3), Enemy(103, "qtx", 800, 1000, 50), Enemy(104, "吕泽玛利亚", 0, 300, 2), Enemy(105, "赵金多", 500, 900, 10), ] # 1. 查找所有死人. 10:44 for item in ListHelper.find_all(list01, lambda e: e.hp == 0): print(item.name) # 2. 查找编号是101的敌人 result = ListHelper.first(list01, lambda e: e.id == 101) print(result.name) # 3. 查找所有活人. for item in ListHelper.find_all(list01, lambda e: e.hp > 0): print(item.name) # 4. 计算所有敌人攻击力总和 result = ListHelper.sum(list01, lambda e: e.atk) print(result) # 5. 查找所有攻击速度在5--10之间的敌人 for item in ListHelper.find_all(list01, lambda e: 5 <= e.atk_speed <= 10): print(item.name) # 6. 查找所有敌人的姓名 # for item in ListHelper.select(list01,lambda e:e.name):
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)