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)
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):