print(item.__dict__) # --------------------------- # 变化 def condition01(emp): return emp.money >= 15000 def condition02(emp): return emp.did == 9005 # 通用 def find(func): for emp in list_employee: # if emp.did == 9005: # if condition02(emp): if func(emp): yield emp # 变化 + 通用 for item in find(condition02): print(item.__dict__) for item in find(condition01): print(item.__dict__) for item in IterableHelper.find_all(list_employee, condition01): print(item.__dict__)
Employee(1003, 9002, "猪八戒", 20000), Employee(1001, 9002, "师父", 60000), Employee(1004, 9001, "沙僧", 30000), Employee(1005, 9001, "小白龙", 15000), ] # 需求:获取所员工的员工编号 for item in IterableHelper.select(list_employees, lambda item: item.eid): print(item) # 1. map映射: for item in map(lambda item: item.eid, list_employees): print(item) # 需求:获取部门编号是9001的所有员工 for item in IterableHelper.find_all(list_employees, lambda emp: emp.did == 9001): print(item.__dict__) # 2. filter过滤: for item in filter(lambda emp: emp.did == 9001, list_employees): print(item.__dict__) max_value = IterableHelper.get_max(list_employees, lambda element: element.money) print(max_value.__dict__) # 3.max 最大 min 最小 max_value = max(list_employees, key=lambda element: element.money) print(max_value.__dict__) # 4.
EmployeeModel(1003, 9003, "刘岳浩", 11000), EmployeeModel(1004, 9007, "冯舜禹", 17000), EmployeeModel(1005, 9005, "曹海欧", 15000), EmployeeModel(1006, 9005, "魏鑫珑", 12000), ] # def condition01(emp): # return emp.money >= 15000 # # def condition02(emp): # return emp.did == 9005 # 通用 def find(func): for emp in list_employee: if func(emp): yield emp # 变化 + 通用 for item in find(lambda emp: emp.did == 9005): print(item.__dict__) for item in find(lambda emp: emp.money >= 15000): print(item.__dict__) for item in IterableHelper.find_all(list_employee, lambda emp: emp.money >= 15000): print(item.__dict__)
] # def condition01(emp): # return emp.money >= 15000 # # def condition02(emp): # return emp.did == 9005 # 通用 def find(func): for emp in list_employee: if func(emp): yield emp # 变化 + 通用 # for item in find_all(condition02): # print(item.__dict__) for item in IterableHelper.find_all(list_employee, lambda item: item.money >= 15000): print(item.__dict__) # for item in find_all(condition01): # print(item.__dict__) print() for item in IterableHelper.find_all(list_employee, lambda item: item.did == 9005): print(item.__dict__)
list_phones = [ Phone("华为1", 5999, "蓝色"), Phone("华为2", 6999, "红色"), Phone("苹果1", 9999, "金色"), Phone("苹果2", 7999, "白色"), Phone("三星2", 4999, "白色"), ] # 将查找条件定义在函数中 # item.color == "白色" # def condition01(item): # return item.color == "白色" # for item in IterableHelper.find_all(list_phones,condition01): # 调用通用函数 for item in IterableHelper.find_all(list_phones, lambda item: item.color == "白色"): print(item.__dict__) # def condition02(item): # return item.brand == "苹果2" # # result = IterableHelper.find_single(list_phones, condition02) # print(result.__dict__) result = IterableHelper.find_single(list_phones, lambda item: item.brand == "苹果2") print(result.__dict__)
# find01() def find02(): for item in list01: if item % 3 == 0 or item % 5 == 0: yield item def cond01(num): return num % 2 != 0 def cond02(num): return num % 3 == 0 or num % 5 == 0 def find_all(func): for item in list01: if func(item): yield item for i in find_all(lambda a: a % 2 != 0): print(i) for i in find_all(lambda num: num % 3 == 0 or num % 5 == 0): print(i) IterableHelper.find_all(list01, lambda a: a % 2 != 0)
yield commodity # for commodity in get_commodity_price_less_10000(): # print(commodity.__dict__) # 变化的 def condition01(com): return com.cid > 1004 def condition02(commodity): return commodity.price < 10000 """ # 不变的 def find_all(list_target, func): for commodity in list_target: # if commodity.price < 10000: # if condition02(commodity): if func(commodity): yield commodity # 连接的: 不变的(变化的) for item in find_all(list_commodity_infos, condition01): print(item.__dict__) """ for item in IterableHelper.find_all(list_commodity_infos, condition01): print(item.__dict__)
""" 3. 使用IterableHelper完成下列功能 list01 = [3,43,4,56,6,76,87,9] -- 在list01中找出所有大于10的数字 -- 在list01中找出第一个偶数 -- 在list01中找出最大的数字 -- 对list01进行升序排列 """ from common.iterable_tools import IterableHelper list01 = [3, 43, 4, 56, 6, 76, 87, 9] for item in IterableHelper.find_all(list01, lambda number: number > 10): print(item) result = IterableHelper.find_single(list01, lambda number: number % 2 == 0) print(result) result = IterableHelper.get_max(list01, lambda number: number) print(result) IterableHelper.order_by(list01,lambda number: number) print(list01)
print(number) for number in find02(): print(number) # ------------------ # 提取变化函数 def condition01(item): return item > 10 def condition02(item): return item % 2 == 0 # 提取通用函数 -- 万能查找 def find(func):# 创建了钩子 for item in list01: # if item % 2 == 0: # if condition02(item): if func(item):# 拉起钩子(执行条件) yield item for number in find(condition02):# 向钩子上挂条件 print(number) from common.iterable_tools import IterableHelper for number in IterableHelper.find_all(list01, condition01): print(number)
Commodity(1002, "倚天剑", 10000), Commodity(1003, "金箍棒", 52100), Commodity(1004, "口罩", 20), Commodity(1005, "酒精", 30), ] for item in map(lambda item: (item.name, item.price), list_commodity_infos): print(item) for item in IterableHelper.select(list_commodity_infos, lambda item: (item.name, item.price)): print(item) for item in filter(lambda item: item.price < 10000, list_commodity_infos): print(item.__dict__) for item in IterableHelper.find_all(list_commodity_infos, lambda item: item.price < 10000): print(item) new_list = sorted(list_commodity_infos, key=lambda item: item.price, reverse=True) print(new_list) tuple01 = ([1, 1], [2, 2, 2], [3, 3, 3]) print(max(tuple01, key=lambda item: len(item))) print(IterableHelper.get_max(tuple01, lambda item: len(item)))
""" """ from common.iterable_tools import IterableHelper def condition01(item): return item % 2 == 0 def condition02(item): return item > 10 def condition03(item): return item < 50 list01 = [43, 4, 5, 56, 76, 78, 8] # 查找所有偶数 # 惰性操作 - 节省内存 for item in IterableHelper.find_all(list01, condition01): print(item) # 立即操作 - 操作灵活 result = list(IterableHelper.find_all(list01, condition01)) print(result[0]) print(result[-1]) print(result[-3:])
def condition02(item): return item.money < 10000 # 万能查找 # def find(func): # for item in list_wifes: # # if item.money < 10000: # # if condition02(item): # if func(item): # yield item # for item in find(condition02): # print(item.__dict__) for item in IterableHelper.find_all(list_wifes, condition01): print(item.__dict__) for item in IterableHelper.find_all(list_wifes, lambda item: item.face_score > 90): print(item.__dict__) # 需求2:定义函数,在老婆列表中,查找姓名是苏荃的老婆对象 # 定义函数,在老婆列表中,查找颜值是100的老婆对象(如有多个返回第一个) # def find01(): # for item in list_wifes: # if item.name == "苏荃": # return item # # def find02(): # for item in list_wifes:
self.name = name self.atk_rate = atk_rate self.cost_sp = cost_sp self.duration = duration list_skills = [ Skill("横扫千军", 1, 50, 5), Skill("九阳神功", 3, 150, 6), Skill("降龙十八掌", 3, 150, 5), Skill("一阳指", 1.2, 0, 2), Skill("乾坤大挪移", 3.2, 30, 2), Skill("打狗棍", 1.3, 0, 6), ] """ -- 在技能列表中查找名称是"一阳指"的技能对象 -- 在技能列表中查找攻击比例atk_rate大于1的所有技能对象 -- 在技能列表中所有技能名称name和消耗法力cost_sp """ skill = IterableHelper.find_single(list_skills, lambda item: item.name == "一阳指") print(skill.__dict__) for skill in IterableHelper.find_all(list_skills, lambda item: item.atk_rate > 1): print(skill.__dict__) for item in IterableHelper.select(list_skills, lambda item: (item.name, item.cost_sp)): print(item)
Wife("苏荃", 98, 10000, 30), Wife("阿珂", 100, 6000, 23), Wife("铁锤", 80, 0, 35), ] # def condition1(item): # return item.face_score > 90 # # # def condition2(item): # return item.money < 100000 # # # def find(func): # for item in list_wifes: # if func(item): # yield item.__dict__ # # # for item in find(condition1): # print(item) # # for item in find(condition2): # print(item) for i in IterableHelper.find_all(list_wifes, lambda item: item.face_score > 90): print(i) for i in IterableHelper.find_all(list_wifes, lambda item: item.money < 100000): print(i)
隔:使用参数隔离具体变化的函数 做:通常使用lambda定义变化函数 练习: """ list01 = [42, 45, 5, 66, 7, 89] # 提取变化函数 # def condition01(item): # return item > 10 # # def condition02(item): # return item % 2 == 0 # 提取通用函数 -- 万能查找 def find(func): # 创建了钩子 for item in list01: if func(item): # 拉起钩子(执行条件) yield item # for number in find_all(condition02):# 向钩子上挂条件 for number in find(lambda item: item % 2 == 0): # 向钩子上挂条件 print(number) from common.iterable_tools import IterableHelper # for number in IterableHelper.find_all(list01, condition01): for number in IterableHelper.find_all(list01, lambda item: item % 2 == 0): print(number)
""" lambda应用 """ from common.iterable_tools import IterableHelper list01 = [63, 5, 56, 7, 8, 9] def condtion01(number): return number < 10 # lambda number:number < 10 # 查找能被5整除的数字 def condition02(item): return item % 5 == 0 for item in IterableHelper.find_all(list01, lambda number: number < 10): print(item) for item in IterableHelper.find_all(list01, lambda item:item % 5 ==0): print(item)
""" 练习: 将find_all封装到common/iterable_tools的 的IterableHelper类中作为静态方法 实现下列效果: 在列表中查找所有字符串 在列表中查找所有大于20的数字 """ from common.iterable_tools import IterableHelper # 参数是列表的每个元素 # 返回值是查找的条件 def condition01(item): return type(item) == str def condition02(item): return type(item) in (int, float) and item > 20 list01 = [43, 45, "56", 67, "78", 9] for item in IterableHelper.find_all(list01, condition01): print(item) for item in IterableHelper.find_all(list01, condition02): print(item)
# -- 在技能列表中查找名称是"一阳指"的技能对象 # -- 在技能列表中查找攻击比例atk_rate大于1的所有技能对象 # -- 在技能列表中所有技能名称name和消耗法力cost_sp from common.iterable_tools import IterableHelper class Skill: def __init__(self, name="", atk_rate=0.0, cost_sp=0, duration=0): self.name = name self.atk_rate = atk_rate self.cost_sp = cost_sp self.duration = duration list_skills = [ Skill("横扫千军", 1, 50, 5), Skill("九阳神功", 3, 150, 6), Skill("降龙十八掌", 3, 150, 5), Skill("一阳指", 1.2, 0, 2), Skill("乾坤大挪移", 3.2, 30, 2), Skill("打狗棍", 1.3, 0, 6), ] if __name__ == '__main__': employee = IterableHelper.find_single(list_skills, lambda emp: emp.name == "一阳指") print(employee.__dict__) for item in IterableHelper.find_all(list_skills, lambda emp: emp.atk_rate > 1): print(item.__dict__) for item in IterableHelper.select(list_skills,lambda emp:(emp.name,emp.cost_sp)): print(item)