# 导入模块成功的唯一条件: # sys.path + 导入路径 可以正确定位模块 import sys sys.path.append('/home/tarena/month01/day15/my_project') print(sys.path) from common.list_helper import ListHelper class SkillDeployer: def generate_skill(self): print("SkillDeployer -- generate_skill") ListHelper.fun01()
import sys sys.path.append("/home/tarena/month01/day15/my_project") print(sys.path) from common.list_helper import ListHelper class SkillDeployer: def func02(self): print("SkillDeployer - func02") # 类名.类方法 ListHelper.func03()
# 对象 --》 字符串 def __str__(self): return self.name list01 = [ Skill(101, "乾坤大挪移", 8000, 30), Skill(102, "九阳神功", 9000, 50), Skill(103, "九阴白骨爪", 500, 10), Skill(104, "黑虎掏心", 9800, 40), Skill(105, "葵花宝典", 6000, 2), ] # 1. tuple01 = ([1, 1], [2, 2, 2, 2], [3, 3]) print(ListHelper.get_max(tuple01, lambda item: len(item))) print(max(tuple01, key=lambda item: len(item))) # 2. for item in ListHelper.select( list01, lambda item: (item.name, item.duration, item.atk)): print(item) for item in map(lambda item: (item.name, item.duration, item.atk), list01): print(item) # 3. print(min(list01, key=lambda item: item.atk)) # 4. for item in sorted(list01, key=lambda item: item.duration, reverse=True): print(item)
def __init__(self, name, atk, cd, speed): self.name = name self.atk = atk self.cd = cd self.speed = speed list01 = [ Skills("横扫千军", 100, 0, 8), Skills("怒气爆发", 5, 0, 2), Skills("天崩地裂斩", 50, 2, 6), Skills("破釜沉舟", 1, 3, 3), Skills("亢龙有悔", 60, 4, 1), Skills("如来神掌", 110, 4, 1), Skills("如来神掌", 120, 4, 1) ] for item in ListHelper.find(list01, lambda item: item.name == "如来神掌"): print(item.name) # list02=[] # for item in ListHelper.find(list01,lambda item:item.atk>50): # list02.append(item) # print(len(list02)) # print(ListHelper.find_count(list01,lambda item:item.cd==0)) # print(ListHelper.find_sum(list01,lambda item:item.cd)) # print(ListHelper.find_max(list01,lambda item:item.atk)) # print(ListHelper.find_min(list01,lambda item:item.atk)) # print(ListHelper.find_list(list01,lambda item:{item.name:item.atk})) ListHelper.find_sheng(list01, lambda item: item.atk) for i in list01: print(i.atk)
print(item, end='\t') # TODO # 獲取持續時間大於10的所有技能 # 獲取編號在102~105之間的所有技能 """ 函數式編成思想 """ def condition01(item): return item.durationd > 10 def condition(item): return 102 <= item.id <= 105 def find(func): for item in list01: if func(item): yield item # for item in find(condition01): # print(item) print("\n-------------ListHelper----------") from common.list_helper import ListHelper for item in ListHelper.find_all(list01, condition01): print(item)
] #函数体 查找多个元素 def condition01(item): return item.duration > 10 def condition02(item): return 102 <= item.id <=105 #函数体 查找单个元素 def condition03(item): return item.id == 102 def condition04(item): return item.name == '九阳神功' #调用函数查找多个元素 for item in ListHelper.find_all(list01,condition01): print(item) #调用函数查找单个元素 re = ListHelper.find_single(list01,condition03) print(re.name) #将调用函数方法改为lambda for item in ListHelper.find_all(list01,lambda item:item.duration >10): print(item) re = ListHelper.find_single(list01,lambda item:item.id == 102) print(re.name) #------------------------------- #1.计算所有攻击力之和 # def sum_atk(): # sum_atk = 0 # for i in range(len(list01)):
# from skill_system import skill_deployer # s01 = skill_deployer.SKillDeployer() # s01.deployer() from skill_system.skill_deployer import SKillDeployer # 创建对象 s01 = SKillDeployer() # 调用实例方法 s01.deployer() # from common import list_helper # list_helper.ListHelper.find_all() from common.list_helper import ListHelper # 通过类名调用静态方法 ListHelper.find_all()
# 封装 def condition01(item): return item.atk > 500 def condition02(item): return len(item.name) > 3 def condition03(item): return 5 < item.duration < 10 # 多态 # 练习一 根据指定条件求和 print(ListHelper.get_num(skill_list, lambda item: item.atk > 500)) print(ListHelper.get_num(skill_list, lambda item: len(item.name) > 3)) print(ListHelper.get_num(skill_list, lambda item: 5 < item.duration < 10)) def condition04(item): return item.id == 103 def condition05(item): return item.name == "乾坤大挪移" def condition06(item): return item.atk > 10
for item in skill_list: if item.duration > 5: return item # 封装 def condition01(item): return item.id == 101 def condition02(item): return item.name == "乾坤大挪移" def condition03(item): return item.duration > 5 # 继承 def find(func): for item in skill_list: if func(item): return item # 多态 re = ListHelper.find_single(skill_list, condition03) print(re.name)