class Student:
"""
学生类:具有name,sex,age,score属性
"""
__slots__ = ["__name", "__sex", "__age", "__score"]
def __init__(self, name, sex, age, score):
self.name = name
self.sex = sex
self.age = age
self.score = score
@property
def name(self):
return self.__name
@name.setter
def name(self, name):
if isinstance(name, str):
self.__name = name
else:
raise TypeError("name is a instrance of class str")
@property
def sex(self):
return self.__sex
@sex.setter
def sex(self, sex):
if isinstance(sex, str):
sex = sex.lower()
if sex in ["f", "m", "male", "female"]:
if sex in ["f", "female"]:
self.__sex = "female"
else:
self.__sex = "male"
return
raise ValueError("sex should be in [f , m , male, female]")
@property
def age(self):
return self.__age
@age.setter
def age(self, age):
if isinstance(age, int) and 6 <= age <= 18:
self.__age = age
else:
raise ValueError("age should be a integer between 6 and 18")
@property
def score(self):
return self.__score
@score.setter
def score(self, score):
if isinstance(score, int) and 0 <= score <= 100:
self.__score = score
else:
raise ValueError("score should be a integer between 0 and 100")
def __str__(self):
return "姓名:%s 性别:%s 年龄:%d 分数:%d" % (self.name, self.sex, self.age, self.score)
class Tool:
@staticmethod
def find_all(list_target, func_condition):
"""
对传入列表进行筛选,获取满足指定条件的所有列表项的生成器
:param list_target: 列表
:param func_condition: 筛选条件
:return: 返回满足筛选条件的生成器
"""
for target in list_target:
if func_condition(target):
yield target
@staticmethod
def find_one(list_target, func_condition):
"""
获取满足筛选条件的一个列表项
:param list_target: 列表
:param func_condition: 筛选条件
:return: 返回满足筛选条件的一个列表项
"""
for target in list_target:
if func_condition(target):
return target
@staticmethod
def count(list_target, func_condition):
"""
计算满足筛选条件的列表项数目
:param list_target: 列表
:param func_condition: 筛选条件
:return: 返回列表中满足筛选条件的列表项数目
"""
i = 0
for target in list_target:
if func_condition(target):
i += 1
return i
@staticmethod
def get_all(list_target, func_condition):
"""
获取列表中满足筛选条件的列表项中某个成员组成的一个列表
:param list_target: 列表
:param func_condition: 筛选条件
:return: 返回列表项中满足条件的列表项成员组成的列表
"""
lst = []
for target in list_target:
lst.append(func_condition(target))
return lst
@staticmethod
def get_max(list_target, func_condition):
"""
获取列表中满足筛选条件的列表项
:param list_target: 列表
:param func_condition: 筛选条件
:return: 返回列表项中某个成员满足最值条件的列表项
"""
if len(list_target) == 0:
return None
if len(list_target) == 1:
return list_target[0]
stud = list_target[0]
for item in list_target[1:]:
stud = func_condition(stud, item)
return stud
@staticmethod
def get_down_sorts(list_target, func_condition):
"""
返回根据筛选条件降序排列的列表项组成的列表
:param list_target: 列表
:param func_condition: 筛选条件
:return: 根据某一筛选条件降序排列的列表
"""
if len(list_target) < 2:
return list_target
sorted_list = []
while len(list_target) > 0:
item = Tool.get_max(list_target, func_condition)
sorted_list.append(item)
list_target.remove(item)
return sorted_list
if __name__ == "__main__":
stulist = [Student("张三", "m", 15, 70), Student("李四", "M", 16, 91), Student("王五", "F", 13, 88),
Student("赵六", "F", 18, 86)]
print("所有学生:")
for stu in stulist:
print(stu)
print("*" * 20)
print("年龄大于13的所有学生")
for stu in Tool.find_all(stulist, lambda student: student.age > 13):
print(stu)
print("*" * 20)
print("分数大于80分的所有学生信息")
for stu in Tool.find_all(stulist, lambda student: student.score > 80):
print(stu)
print("*" * 20)
print("所有女学生信息:")
for stu in Tool.find_all(stulist, lambda student: student.sex == "female"):
print(stu)
print("*" * 20)
print("获取姓名位张三的学生信息:")
stu = Tool.find_one(stulist, lambda student: student.name == "张三")
print(stu)
print("*" * 20)
print("分数大于80的学生人数:", end=" ")
count = Tool.count(stulist, lambda student: student.score > 80)
print(count)
print("*" * 20)
print("女学生人数:", end=" ")
count = Tool.count(stulist, lambda student: student.sex == "female")
print(count)
print("所有学生姓名:", end=" ")
namelist = Tool.get_all(stulist, lambda student: student.name)
print(namelist)
print("*" * 20)
print("成绩最高的学生信息:")
stu = Tool.get_max(stulist, lambda student1, student2: student1 if student1.score > student2.score else student2)
print(stu)
print("*" * 20)
print("年龄最大的学生:")
stu = Tool.get_max(stulist, lambda student1, student2: student1 if student1.age > student2.age else student2)
print(stu)
print("*" * 20)
print("按成绩降序输出学生信息:")
sorted_list = Tool.get_down_sorts(stulist, lambda student1,
student2: student1 if student1.score > student2.score else student2)
for student in sorted_list:
print(student)
|