Example #1
0
    Commodity(1005, "酒精", 30),
    Commodity(1006, "屠龙刀", 10000),
    Commodity(1007, "口罩", 50),
]


def find01():
    for item in list_commodity_infos:
        if item.cid == 1002:
            return item


def find02():
    for item in list_commodity_infos:
        if item.name == "金箍棒":
            return item


# def condition01(item):
#     return item.cid == 1002

# def condition02(item)
#     return item.name == "金箍棒"

# re = IterableHelper.find_single(list_commodity_infos, condition02)
# print(re.__dict__)


re = IterableHelper.find_single(list_commodity_infos,
                                lambda item:item.name == "金箍棒")
print(re.__dict__)
Example #2
0
    return count


print(get_count(condition05))

# 调用静态方法
for itme in IterableHelper.find(list_wifes, condition01):
    print(itme.__dict__)

print(IterableHelper.get_count(list_wifes, condition05))

# 使用lambda表达式
for itme in IterableHelper.find(list_wifes, lambda item:item.height > 170):
    print(itme.__dict__)

print(IterableHelper.find_single(list_wifes,lambda item:item.name == "双儿").__dict__)

print(IterableHelper.get_count(list_wifes, lambda item:item.face_score > 90))

# 练习4:
#     需求1:在老婆列表中查找所有老婆的姓名与颜值
for item in IterableHelper.select(list_wifes,lambda wife:(wife.name,wife.face_score)):
    print(item)
#     需求2:在老婆列表中查找所有老婆的身高、体重、颜值
for item in IterableHelper.select(list_wifes,lambda wife:(wife.height,wife.weight,wife.face_score)):
    print(item)

# 练习5:
#     需求1:在老婆列表中查找颜值最高的老婆
print(IterableHelper.get_max(list_wifes, lambda element: element.face_score).__dict__)
#     需求2:在老婆列表中查找财产最大的老婆
Example #3
0
def find02():
    for item in list_commodity_infos:
        if item.name == "金箍棒":
            return item


def condition01(item):
    return item.cid == 1002


def condition02(item):
    return item.name == "金箍棒"


"""
# 参数是列表,表示数据灵活.
# 参数是函数,表示逻辑灵活.
def find_single(list_target,func):
    for item in list_target:
        # if item.name == "金箍棒":
        # if condition01(item):
        if func(item):
            return item

re =find_single(list_commodity_infos,condition02)
print(re.__dict__)
"""
re = IterableHelper.find_single(list_commodity_infos, condition02)
print(re.__dict__)
Example #4
0
        self.eid = eid  # 员工编号
        self.did = did  # 部门编号
        self.name = name
        self.money = money

    def __str__(self):
        return f"{self.name}的员工编号是{self.eid},部门编号是{self.did},工资是{self.money}"


# 员工列表
list_employees = [
    Employee(1001, 9002, "师父", 60000),
    Employee(1002, 9001, "孙悟空", 50000),
    Employee(1003, 9002, "猪八戒", 20000),
    Employee(1004, 9001, "沙僧", 30000),
    Employee(1005, 9001, "小白龙", 15000),
]


def find_eid(item):
    return item.eid == 1003


def find_name(item):
    return item.name == "孙悟空"


result_eid = IterableHelper.find_single(list_employees, lambda item: item.eid ==1003)
print(result_eid.__dict__)
result_name = IterableHelper.find_single(list_employees, lambda item: item.name == "孙悟空")
print(result_name.__dict__)
from common.iterable_tools import IterableHelper


class Wife:
    def __init__(self, name, face_score, money, age):
        self.name = name
        self.face_score = face_score
        self.money = money
        self.age = age


list_wives = [
    Wife("建宁", 86, 999999, 25),
    Wife("双儿", 100, 5000, 23),
    Wife("苏荃", 98, 10000, 30),
    Wife("阿珂", 100, 6000, 23),
    Wife("铁锤", 80, 0, 35),
]

print(IterableHelper.find_single(list_wives, lambda item: item.name == "苏荃"))

print(
    IterableHelper.find_single(list_wives,
                               lambda item: item.face_score == 100))
Example #6
0

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__)
Example #7
0
"""
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)
Example #8
0
def find_single01():
    for item in list_employees:
        if item.eid == 1001:
            return item


def find_single02():
    for item in list_employees:
        if item.name == "孙悟空":
            return item


def condtion01(item):
    return item.eid == 1001


def condtion02(item):
    return item.name == "孙悟空"


def find_single(func):
    for item in list_employees:
        # if item.name == "孙悟空":
        # if condtion01(item):
        # if condtion02(item):
        if func(item):
            return item

emp = IterableHelper.find_single(list_employees,condtion01)
print(emp.__dict__)
    return emp.name == "刘岳浩"


def condition02(emp):
    return emp.eid == 1005


def find(func):
    for emp in list_employee:
        # if emp.eid == 1005:
        # if condition02(emp):
        if func(emp):
            return emp


emp01 = IterableHelper.find_single(list_employee, lambda emp: emp.name == "刘岳浩")
print(emp01.__dict__)


# 2.
def select01():
    for emp in list_employee:
        yield emp.name

def select02():
    for emp in list_employee:
        yield (emp.eid, emp.money)


def handle01(emp):
    return emp.name
Example #10
0
        self.name = name
        self.money = money

    def __str__(self):
        return f"{self.name}的员工编号是{self.eid},部门编号是{self.did},工资是{self.money}"


# 员工列表
list_employees = [
    Employee(1001, 9002, "师父", 60000),
    Employee(1002, 9001, "孙悟空", 50000),
    Employee(1003, 9002, "猪八戒", 20000),
    Employee(1004, 9001, "沙僧", 30000),
    Employee(1005, 9001, "小白龙", 15000),
]


def find_eid(item):
    return item.eid == 1003


def find_name(item):
    return item.name == "孙悟空"


result_eid = IterableHelper.find_single(list_employees, find_eid)
print(result_eid.__dict__)
result_name = IterableHelper.find_single(list_employees,find_name)
print(result_name.__dict__)

Example #11
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),
]
"""
    -- 在技能列表中查找名称是"一阳指"的技能对象
    -- 在技能列表中查找攻击比例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)
Example #12
0
        if item.name == "孙悟空":
            return item


def condition01(item):
    return item.eid == 1001


def condition02(item):
    return item.name == "孙悟空"


def find_single(condition):
    for item in list_employees:
        # if item.name == "孙悟空":
        # if condition02(item):
        # if condition01(item):
        if condition(item):
            return item


def condition03(item):
    return item.name == "小白龙"


re = find_single(condition01)
print(re.__dict__)

re = IterableHelper.find_single(list_employees, condition03)
print(re.__dict__)
Example #13
0
#     -- 在技能列表中查找名称是"一阳指"的技能对象
#     -- 在技能列表中查找攻击比例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)