예제 #1
0
        self.name = name
        self.price = price


list_commodity_infos = [
    Commodity(1001, "屠龙刀", 10000),
    Commodity(1002, "倚天剑", 10000),
    Commodity(1003, "金箍棒", 52100),
    Commodity(1004, "口罩", 20),
    Commodity(1005, "酒精", 30),
    Commodity(1006, "屠龙刀", 10000),
    Commodity(1007, "口罩", 50),
]

# 练习1:
re = IterableHelper.get_min(list_commodity_infos, lambda item: item.price)
print(re.__dict__)


# 练习2:
def order_by01():
    for r in range(len(list_commodity_infos) - 1):
        for c in range(r + 1, len(list_commodity_infos)):
            if list_commodity_infos[r].price > list_commodity_infos[c].price:
                list_commodity_infos[r], list_commodity_infos[c] = list_commodity_infos[c], list_commodity_infos[r]

def order_by02():
    for r in range(len(list_commodity_infos) - 1):
        for c in range(r + 1, len(list_commodity_infos)):
            if list_commodity_infos[r].cid > list_commodity_infos[c].cid:
                list_commodity_infos[r], list_commodity_infos[c] = list_commodity_infos[c], list_commodity_infos[r]
예제 #2
0
def get_min01():
    min_value = list_employees[0]
    for i in range(1, len(list_employees)):
        if min_value.eid > list_employees[i].eid:
            min_value = list_employees[i]
    return min_value


def get_min02():
    min_value = list_employees[0]
    for i in range(1, len(list_employees)):
        if min_value.money > list_employees[i].money:
            min_value = list_employees[i]
    return min_value


def condition01(item):
    return item.money


def get_min(condition01):
    min_value = list_employees[0]
    for i in range(1, len(list_employees)):
        if condition01(min_value) > condition01(list_employees[i]):
            min_value = list_employees[i]
    return min_value


print(IterableHelper.get_min(list_employees, lambda item: item.eid).name)
print(IterableHelper.get_min(list_employees, lambda item: item.money).name)
예제 #3
0
class Employee:
    def __init__(self, eid, did, name, money):
        self.eid = eid  # 员工编号
        self.did = did  # 部门编号
        self.name = name
        self.money = money

    # 通过重写方法获取最值,只适用于最常见的条件
    # min(list_employees)
    # 如果条件过多,通过函数式编程实现.
    # 自定义函数(list_employees,lambda emp:emp.money
    def __lt__(self, other):
        return self.money < other.money


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

min_value = IterableHelper.get_min(list_employees, lambda emp: emp.money)
print(min_value.__dict__)

min_value = min(list_employees)
print(min_value.__dict__)
예제 #4
0
 def get_min_area(self):
     return IterableHelper.get_min(self.__list_houses, lambda item: item.area)