SkillData(105, "冷酷追击", 15, 30, 9), ] # 不变的 # def find_demo04(target,func_condition): # for item in target: # if func_condition(item): # yield item # 变化的 def condition01(item): return item.cd == 0 def condition02(item): return item.atk > 5 def condition03(item): return item.atk > 10 and item.costSP == 0 # for item in find_demo04(list_skills,condition01): # print(item) # 通过模块调用 from common.custom_list_tools import ListHelper for item in ListHelper.find_all(list_skills, condition01): print(item)
import sys from common.custom_list_tools import ListHelper class Skill: def __init__(self, id, name, cd, atk, magic): self.id = id self.name = name self.cd = cd self.atk = atk self.magic = magic list_skills = [ Skill(101, "降龙十八掌", 2, 130, 15), Skill(102, "六脉神剑", 0, 100, 35), Skill(103, "一阳指", 2, 190, 0), Skill(104, "葵花点穴手", 0, 120, 15), Skill(105, "韦陀掌", 5, 90, 0), ] from common.custom_list_tools import ListHelper sum_id = ListHelper.sum(list_skills, lambda item: item.id) print(sum_id) sum_cd = ListHelper.sum(list_skills, lambda item: item.cd) print(sum_cd) print(sys.path) print(__name__)
# 技能列表 -- 转换为编号列表 # 技能列表 -- 转换为名称列表 from common.custom_list_tools import ListHelper class Skill: def __init__(self, id, name, cd, atk, magic): self.id = id self.name = name self.cd = cd self.atk = atk self.magic = magic list_skills = [ Skill(101, "降龙十八掌", 2, 130, 15), Skill(102, "六脉神剑", 0, 100, 35), Skill(103, "一阳指", 2, 190, 0), Skill(104, "葵花点穴手", 0, 120, 15), Skill(105, "韦陀掌", 5, 90,0), ] from common.custom_list_tools import ListHelper list01 = list(ListHelper.select(list_skills, lambda item: item.id)) print(list01) list02 = list(ListHelper.select(list_skills, lambda item: item.name)) print(list02) list03 = list(ListHelper.select(list_skills, lambda item: (item.id, item.name))) print(list03)
# ------------------------- a01 = lambda: print("我是lambda方法") a01() a02 = lambda a: print("我是lambda方法,参数是,", a) a02(500) a03 = lambda: True print(a03()) #------------------------------------ from common.custom_list_tools import ListHelper list01 = [1, 2, 33, 4, 45, 6] for item in ListHelper.find_all(list01, lambda item: item > 5): print(item) for item in ListHelper.find_all(list01, lambda item: item % 2 != 0): print(item) # 提取不同点: # def condition01(item): # return item> 5 # # def condition02(item): # return item % 2 != 0 # # def condition03(item): # return item < 3
if item.atk > 10 and item.costSP == 0: yield item # 提取共同点 def find_all(target, condition): for item in target: if condition(item): yield item # 提取变化点 def condition01(item): return item.cd == 0 def condition02(item): return item.atk > 5 def condition03(item): return item.atk > 10 and item.costSP == 0 from common.custom_list_tools import ListHelper ListHelper.find_all(list01, condition02())
def fun03(): return True a03 = lambda: True print(a03()) # 应用 from common.custom_list_tools import ListHelper list01 = [1, 2, 33, 4, 45, 6] # 提取不同点 def condition01(item): return item > 5 def condition02(item): return item % 2 != 0 def condition03(item): return item < 3 for item in ListHelper.find_all(list01, condition01): print(item) for item in ListHelper.find_all(list01, lambda item: item < 3): print(item)
] # 变化的 # def condition01(item): # return item.cd == 0 # # def condition02(item): # return item.atk > 5 # # def condition03(item): # return item.atk > 10 and item.costSP == 0 # 通过模块调用 from common.custom_list_tools import ListHelper for item in ListHelper.find_all(list_skills, lambda item: item.cd == 0): print(item) for item in ListHelper.find_all(list_skills, lambda item: item.atk > 5): print(item) # 练习:16:30 # 解决的问题: # 查找编号是101的(单个)技能对象 # 查找名称是"降龙十八掌"的(单个)技能对象 # 查找cd大于10的(单个)技能对象 # 解决的步骤: # 1. 逐个解决问题 # 2. 将共性提取到ListHelper中 # 3. 将变化用lambda表示.
# 练习:参照下列代码,实现对技能列表的查找 from common.custom_list_tools import ListHelper list01 = [45, 12, 6, 22, 56] # for val in ListHelper.find_all(list01, lambda item: item > 20): # print(val) # # for val in ListHelper.find_all(list01, lambda item: item >30 and item%2 == 0): # print(val) for val in ListHelper.find_all(list01, lambda item: item > 30 and item % 2 != 0): print(val)