Exemplo n.º 1
0
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的敌人
result = ListHelper.include(L, lambda enemy: enemy.hp == 0)
print(result)
Exemplo n.º 2
0
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(result)
print()

#     5. 查找所有攻击速度在5--10之间的敌人
for item in ListHelper.find_all(L, lambda enemy: 5 <= enemy.atk_speed <= 10):
Exemplo n.º 3
0
        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):
#     print(item)

# 结果是生成器对象