コード例 #1
0
ファイル: bll.py プロジェクト: mangodayup/month01-resource
 def get_house_by_max_total_price(self):
     # 写法1:简单但不灵活
     # 重写模型的__gt__方法
     # return max(self.__list_houses)
     # 写法2: 内置高阶函数(性能高)
     # return max(self.__list_houses,key = lambda house:house.total_price)
     # 写法3: 自定义高阶函数(调试方便)
     return IterableHelper.get_max(self.__list_houses,
                                   lambda house: house.total_price)
コード例 #2
0
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:在老婆列表中查找财产最大的老婆
print(IterableHelper.get_max(list_wifes, lambda element: element.money).__dict__)

# 练习6:
#     需求1:根据身高对老婆列表进行升序排列
#     需求2:根据体重对老婆列表进行升序排列
IterableHelper.order_by(list_wifes,lambda w:w.height)
IterableHelper.order_by(list_wifes,lambda w:w.weight)
for item in list_wifes:
    print(item.__dict__)

# 练习7:
#     需求1:累加老婆们的体重
#     需求2:累加老婆们的财产
print(IterableHelper.sum_value(list_wifes, lambda w: w.money))
コード例 #3
0
ファイル: demo01.py プロジェクト: zjkliuzy/pythonProject1
    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.
# 自定义排序函数内部修改了列表,所以没有提供返回值
# IterableHelper.order_by(list_employees,lambda item:item.did)
# print(list_employees)

# 内置sorted排序,返回排好序的新列表
# 升序
new_list = sorted(list_employees, key=lambda item: item.did)
# 降序 reverse=True
コード例 #4
0
# count = IterableHelper.delete_all(list_employee,lambda emp:emp.money < 15000)
# print(count)

count = IterableHelper.delete_all(list_employee, lambda item: item.did == 9005)
print(count)

# def get_max01():
#     max_value = list_employee[0]
#     for i in range(1, len(list_employee)):
#         if max_value.money < list_employee[i].money:
#             max_value = list_employee[i]
#     return max_value
#
# def get_max02():
#     max_value = list_employee[0]
#     for i in range(1, len(list_employee)):
#         if max_value.eid < list_employee[i].eid:
#             max_value = list_employee[i]
#     return max_value

max = IterableHelper.get_max(list_employee, lambda item: item.money)
print(max.__dict__)

max = IterableHelper.get_max(list_employee, lambda item: item.eid)
print(max.__dict__)

# 在列表中根据薪资升序排列
IterableHelper.ascending_order(list_employee, lambda emp: emp.money)
IterableHelper.ascending_order(list_employee, lambda emp: emp.eid)
print(list_employee)
コード例 #5
0
def get_count02():
    count = 0
    for phone in list_phones:
        if phone.price > 7000:
            count += 1
    return count
"""

print(IterableHelper.get_count(list_phones, lambda p: p.color == "白色"))

#  [先用] -- 做通用函数时,调用 max_value.price 的函数
#  [后做] -- 用通用函数时,做lambda代替 max_value.price 的函数
"""
def get_max01():
    max_value = list_phones[0]
    for i in range(1, len(list_phones)):
        if max_value.price < list_phones[i].price:
            max_value = list_phones[i]
    return max_value

def get_max02():
    max_value = list_phones[0]
    for i in range(1, len(list_phones)):
        if len(max_value.brand) < len(list_phones[i].brand):
            max_value = list_phones[i]
    return max_value
"""
re = IterableHelper.get_max(list_phones, lambda element: element.price)
re = IterableHelper.get_max(list_phones, lambda element: len(element.brand))
print(re.__dict__)
コード例 #6
0
        self.name = name
        self.money = money


# 员工列表
list_employees = [
    Employee(1001, 9002, "师父", 60000),
    Employee(1002, 9001, "孙悟空", 50000),
    Employee(1003, 9002, "猪八戒", 20000),
    Employee(1004, 9001, "沙僧", 30000),
    Employee(1005, 9001, "小白龙", 15000),
]
"""
def get_max01():
    max_value = list_employees[0]
    for i in range(1,len(list_employees)):
        if max_value.money < list_employees[i].money:
            max_value = list_employees[i]
    return max_value

def get_max02():
    max_value = list_employees[0]
    for i in range(1,len(list_employees)):
        if max_value.eid < list_employees[i].eid:
            max_value = list_employees[i]
    return max_value
"""

emp = IterableHelper.get_max(list_employees, lambda item: item.money)
print(emp.__dict__)
コード例 #7
0
ファイル: exercise01.py プロジェクト: dalaAM/month-01
"""
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)
コード例 #8
0
    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)))
コード例 #9
0
ファイル: exercise05.py プロジェクト: weqq2019/tmooc_cn
#
# def condition01(item):
#     return item.name == "苏荃"
#
# def condition02(item):
#     return item.face_score == 100
#
# def find(func):
#     for item in list_wifes:
#         # if item.face_score == 100:
#         if func(item):
#             return item

result = IterableHelper.find(list_wifes, lambda item: item.name == "苏荃")
print(result.__dict__)

for item in IterableHelper.select(list_wifes, lambda wife: wife.name):
    print(item)

for item in IterableHelper.select(list_wifes, lambda wife:
                                  (wife.name, wife.face_score)):
    print(item)

result = IterableHelper.get_max(list_wifes, lambda wife: wife.money)
print(result.__dict__)

IterableHelper.order_by(list_wifes, lambda wife: wife.face_score)

for item in list_wifes:
    print(item.__dict__)
コード例 #10
0
 def get_max_price(self):
     return IterableHelper.get_max(self.__list_houses, lambda item: item.total_price)