# 获取具有生命值的对象总数 # 获取攻击速度小于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的敌人 result = ListHelper.include(L, lambda enemy: enemy.hp == 0) print(result) result = ListHelper.include(L, lambda enemy: enemy.atk_speed > 10) print(result) #删除所有死人 # ListHelper.del_all(L,lambda enemy : enemy.hp == 0) # print(L) #删除编号是101的敌人 # ListHelper.del_all(L,lambda enemy : enemy.id == 101) # print(L) #获取血量最大的敌人 result = ListHelper.get_max(L, lambda enemy: enemy.atk_speed) print(result) print(L) ListHelper.order_by(L, lambda enemy: enemy.atk_speed) print(L) ListHelper.order_by_descending(L, lambda enemy: enemy.atk_speed) print(L)
# # 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) # 获取攻击力最大的敌人 result = max(list01, key=lambda e: e.atk) print(result.name) result = ListHelper.get_max(list01, lambda e: e.atk) print(result.name)
# 获取血量最大的敌人 # 获取攻击力最强的 # ... # def demo01(target): # max_value = target[0] # for i in range(1, len(target)): # # if max_value.hp < target[i].hp: # if xxx(max_value) < xxx(target[i]): # max_value = target[i] # return max_value # # def xxx(item): # return item.hp max = ListHelper.get_max(list01, lambda e: e.hp) print(max.name) # 练习:14:45 # 解决的问题:根据指定条件升序排列列表 # 按照血量升序排列 # 按照攻击力升序排列 # def demo01(target): # for r in range(len(target)-1): # for c in range(r+1,len(target)): # # if target[r].hp > target[c].hp: # if xxx(target[r]) > xxx(target[c]): # target[r],target[c] = target[c],target[r] # # def demo02(target): # for r in range(len(target)-1):