示例#1
0
def total():
    if os.path.exists(file_name):  # 判断文件是否存在
        with open(file_name, 'r') as rfile:  # 打开文件
            student_old = rfile.readlines()  # 读取全部内容
            amount=IterableHelper.get_count(student_old,lambda student:student)
            print("一共有 %d 名学生!" % amount)
def get_count01():
    count = 0
    for phone in list_phones:
        if phone.color == "白色":
            count += 1
    return count

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):
示例#3
0
def get_count(func):
    count = 0
    for item in list_wifes:
        if func(item):
            count += 1
    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)):
示例#4
0
# def get_count(func):
#     count = 0
#     for item in list_wifes:
#         if func(item):
#             count += 1
#     return count
#
# print(get_count(condition05))

# 调用静态方法
for itme in IterableHelper.find(list_wifes, lambda item: item.height > 70):
    print(itme.__dict__)
# def condition01(item):
#     return item.height > 170
print(IterableHelper.get_count(list_wifes, lambda item: item.height < 170))
# def condition05(item):
#     return item.height < 170

for item in IterableHelper.find(list_wifes, lambda item: item.face_score < 90):
    print(item.__dict__)
# def condition02(item):
#     return item.face_score < 90

# 练习4:
#       需求1:在老婆列表中查找所有老婆的姓名与颜值
#       需求2:在老婆列表中查找所有老婆的身高、体重、颜值

for item in IterableHelper.select(list_wifes, lambda item:
                                  (item.name, item.face_score)):
    print(item)
示例#5
0
def get_count02():
    count = 0
    for item in list_employees:
        if len(item.name) == 2:
            count += 1
    return count

print(get_count01())
 
def condition01(item):
    return item.did == 9001

def condition02(item):
    return len(item.name) == 2

def get_count(condition):
    count = 0
    for item in list_employees:
        # if len(item.name) == 2:
        # if condition02(item):
        # if condition01(item):
        if condition(item):
            count += 1
    return count
 
print(get_count(condition02))
"""

print(IterableHelper.get_count(list_employees, lambda item: item.did == 9002))
示例#6
0
value  = get_count02()
print(value)

# 分 - 变化
# 火车
def condition01(item):
    return item.money > 20000

# 汽车
def condition02(item):
    return item.did == 9002

# 分 - 不变
def get_count(func):
    count = 0
    for item in list_employees:
        # if item.did == 9002:
        # if condition02(item):
        # if condition01(item):
        # 先确定用法
        if func(item):
            count += 1
    return count
"""

value = IterableHelper.get_count(list_employees,
                                 lambda item: item.money > 20000)
print(value)

IterableHelper.order_by(list_employees, lambda item: item.money)
print(list_employees)
示例#7
0
        if len(item.name) > 2:
            count += 1
    return count


"""
# 提取不变的
def get_count(list_target, func):
    count = 0
    for item in list_target:
        # if item.price > 10000:
        if func(item):  # 使用参数(传入的函数 lambda)代替具体的逻辑判断
            count += 1
    return count

# 脑补
# def xxx(item):
#     return item.price > 10000

# lambda 参数是列表的每个元素,函数体是对每个元素的判断逻辑.
result = get_count(list_commodity_infos, lambda commodity: commodity.price >= 10000)
print(result)

result = get_count(list_commodity_infos, lambda commodity: len(commodity.name) > 2)
print(result)

"""
result = IterableHelper.get_count(list_commodity_infos,
                                  lambda commodity: commodity.price >= 10000)
print(result)
#     for item in list_employees:
#         if item.money > 20000:
#             total_count += 1
#     return total_count
#
#
# def get_count02():
#     total_count = 0
#     for item in list_employees:
#         if item.did == 9001:
#             total_count += 1
#     return total_count
#
#
# def condition01(item):
#     return item.money > 20000
#
# def coudition02(item):
#     return item.did == 9001
#
# def get_count(func):
#     total_count = 0
#     for item in list_employees:
#         if func(item):
#             total_count += 1
#     return total_count

print(IterableHelper.get_count(list_employees,
                               lambda item: item.money > 20000))
print(IterableHelper.get_count(list_employees, lambda item: item.did == 9001))