Ejemplo n.º 1
0
def sort():
    show()
    if os.path.exists(file_name):
        with open(file_name, 'r') as file:  # 打开文件
            student_old = file.readlines()  # 读取全部内容
            student_new = []
        for list in student_old:
            d = dict(eval(list))  # 字符串转字典
            student_new.append(d)  # 将转换后的字典添加到列表中
    else:
        return
    ascORdesc = input("请选择(0升序;1降序):")
    if ascORdesc == "0":  # 按升序排序
        ascORdescBool = False  # 标记变量,为False表示升序排序
    elif ascORdesc == "1":  # 按降序排序
        ascORdescBool = True  # 标记变量,为True表示降序排序
    else:
        print("您的输入有误,请重新输入!")
        sort()
    mode = input("请选择排序方式(1按英语成绩排序;2按Python成绩排序;3按C语言成绩排序;0按总成绩排序):")
    if mode == "1":  # 按英语成绩排序
        IterableHelper.order_by_descending(student_new,lambda x: x["python"])
    elif mode == "2":  # 按Python成绩排序
        student_new.sort(key=lambda x: x["python"], reverse=ascORdescBool)
    elif mode == "3":  # 按C语言成绩排序
        student_new.sort(key=lambda x: x["c"], reverse=ascORdescBool)
    elif mode == "0":  # 按总成绩排序
        student_new.sort(key=lambda x: x["english"] + x["python"] + x["c"], reverse=ascORdescBool)
    else:
        print("您的输入有误,请重新输入!")
        sort()
    show_student(student_new)  # 显示排序结果
Ejemplo n.º 2
0
 def __select_menu(self):
     select = input_select("选项")
     if select == 1:
         self.__show_house_informaitin()
     elif select == 2:
         # house = self.__control.get_house_max_total_price()
         # house = IterableHelper.get_max(self.__control.get_house_list(),lambda house: house.total_price)
         house = max(self.__control.get_house_list(),
                     key=lambda house: house.total_price)
         print(house.__dict__)
     elif select == 3:
         # house = self.__control.get_house_min_area()
         # house = min(self.__control.get_house_list(), key=lambda house: house.area)
         house = min(self.__control.get_house_list(),
                     key=lambda house: house.id)
         print(house.__dict__)
     elif select == 4:
         print(self.__control.count_house_type())
         pass
     elif select == 5:
         id = input_select("房源编号")
         # if self.__control.del_house_by_id(id):
         if IterableHelper.del_single_by_info(self.__control.__list_houses,
                                              lambda house: house.id == id):
             print("删除成功")
         else:
             print("删除失败,没找到此房源")
     elif select == 6:
         self.__control.ascending_ording()
         for house in sorted(self.__control.__list_houses,
                             key=lambda house: house.total_price):
             print(house.__dict__)
Ejemplo n.º 3
0
 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)
Ejemplo n.º 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__)
Ejemplo n.º 5
0
    Wife("铁锤", 27, 190, 200),
    Wife("铁钉", 37, 165, 160),
    Wife("铁棒", 24, 160, 190),
    Wife("铁锅", 23, 190, 100),
]


def condition01(item):
    return item.age > 25


def condition02(item):
    return item.height < 180


for item in IterableHelper.find_all(list01, condition01):
    print(item)

# ---------------------练习1-----------------------------
"""
def select01():
    for item in list01:
        yield item.name

def select02():
    for item in list01:
        yield (item.name,item.height)
"""


def handle01(item):
Ejemplo n.º 6
0
            隔:使用参数隔离具体变化的函数
            做:通常使用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)
Ejemplo n.º 7
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__)
Ejemplo n.º 8
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]
Ejemplo n.º 9
0
            定义函数,根据薪资对员工列表进行降序排列
            定义函数,根据编号对员工列表进行降序排列
        步骤:
            1.在IterableHelper中创建
               通用函数 is_repeat
            2. 使用lambda在当前模块中调用
"""
from common.iterable_tools import IterableHelper


class Employee:
    def __init__(self, eid, did, name, money):
        self.eid = eid  # 员工编号
        self.did = did  # 部门编号
        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),
]

IterableHelper.descending_by(list_employees, lambda item: item.money)

print(list_employees)
Ejemplo n.º 10
0
]

# 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__)
Ejemplo n.º 11
0
        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_all_name():
    for item in list_employees:
        yield item.name


def find_all_eid_money():
    for item in list_employees:
        yield item.eid, item.money


result01 = IterableHelper.select(list_employees, lambda item: item.name)
result02 = IterableHelper.select(list_employees, lambda item:
                                 (item.eid, item.money))
for item in result01:
    print(item)
for item in result02:
    print(item)
Ejemplo n.º 12
0
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))
Ejemplo n.º 13
0
    for item in list_commodity_infos:
        yield item.name


for name in select01():
    print(name)


def select02():
    for item in list_commodity_infos:
        yield item.cid, item.price


for name in select02():
    print(name)
"""
# 提取不变的
def select(list_target,func):
    for item in list_target:
        # yield item.name
        yield func(item) # 调用lambda函数,传递列表的每个元素

for name in select(list_commodity_infos,lambda com:com.name):
    print(name)

for name in select(list_commodity_infos,lambda com:(com.cid,com.price)):
    print(name)
"""
for name in IterableHelper.select(list_commodity_infos, lambda com: com.name):
    print(name)
Ejemplo n.º 14
0
    return count

def delete02():
    count = 0
    for i in range(len(list_employee)-1,-1,-1):
        if list_employee[i].did == 9005:
            del list_employee[i]
            count += 1
    return count
"""

#                                                  参数是列表中的每个元素
# 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
Ejemplo n.º 15
0
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):
Ejemplo n.º 16
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__)
Ejemplo n.º 17
0
    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__)
Ejemplo n.º 18
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__)
Ejemplo n.º 19
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__)
Ejemplo n.º 20
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)
Ejemplo n.º 21
0
def condition06(item):
    return item.face_score > 90


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)):
Ejemplo n.º 22
0
            定义函数,判断列表中是否具有姓名相同的员工
            定义函数,判断列表中是否具有编号相同的员工
        步骤:
            1.在IterableHelper中创建
               通用函数
            2. 使用lambda在当前模块中调用
"""
from common.iterable_tools import IterableHelper


class Employee:
    def __init__(self, eid, did, name, money):
        self.eid = eid  # 员工编号
        self.did = did  # 部门编号
        self.name = name
        self.money = money


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

print(IterableHelper.is_repeat(list_employees,lambda item:item.name))

Ejemplo n.º 23
0
    def __lt__(self, other):
        return self.money < other.money


# 员工列表
list_employees = [
    Employee(1001, 9002, "师父", 60000),
    Employee(1002, 9001, "孙悟空", 50000),
    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__)
Ejemplo n.º 24
0
def condition06(item):
    return item.face_score > 90


# 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:在老婆列表中查找所有老婆的身高、体重、颜值
Ejemplo n.º 25
0
        self.price = price
        self.color = color


list_phones = [
    Phone("华为1", 5999, "蓝色"),
    Phone("华为2", 6999, "红色"),
    Phone("苹果1", 9999, "金色"),
    Phone("苹果2", 7999, "白色"),
    Phone("三星2", 4999, "白色"),
]


def delete_all01():
    for i in range(len(list_phones) - 1, -1, -1):
        if list_phones[i].color == "蓝色":
            del list_phones[i]


def delete_all02():
    for i in range(len(list_phones) - 1, -1, -1):
        if list_phones[i].price < 7000:
            del list_phones[i]


# def condtion01(phone):
#     return phone.color == "蓝色"

IterableHelper.delete_all(list_phones, lambda phone: phone.color == "蓝色")
for item in list_phones:
    print(item.__dict__)
Ejemplo n.º 26
0
    for r in range(len(list_employees) - 1):
        for c in range(r + 1, len(list_employees)):
            if list_employees[r].eid > list_employees[c].eid:
                list_employees[r], list_employees[c] = list_employees[
                    c], list_employees[r]


def conditon01(item):
    return item.money


def conditon02(item):
    return item.eid


def order_by(condition01):
    for r in range(len(list_employees) - 1):
        for c in range(r + 1, len(list_employees)):
            if condition01(list_employees[r]) > condition01(list_employees[c]):
                list_employees[r], list_employees[c] = list_employees[
                    c], list_employees[r]


IterableHelper.order_by(list_employees, lambda item: item.money)
# for item in list_employees:
#     print(item.name)

IterableHelper.order_by(list_employees, lambda item: item.eid)
for item in list_employees:
    print(item)
Ejemplo n.º 27
0
    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__)
Ejemplo n.º 28
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__)
Ejemplo n.º 29
0
"""
    练习:
    将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)
Ejemplo n.º 30
0
        self.did = did  # 部门编号
        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 sum01():
    sum_value = 0
    for item in list_employees:
        sum_value += item.money
    return sum_value

def sum02():
    sum_value = 0
    for item in list_employees:
        sum_value += item.eid
    return sum_value
"""

print(IterableHelper.sum(list_employees, lambda item: item.eid))
print(IterableHelper.sum(list_employees, lambda item: item.money))
print(IterableHelper.sum(list_employees, lambda item: item.did))