python
class Student:
def __init__(self, name, num, age, score):
self.name = name
self.num = num
self.age = age
self.score = score
def __str__(self):
return '姓名:{} 学号:{} 年龄:{} 成绩:{}'.format(self.name, self.num, self.age, self.score)
class Class:
def __init__(self, name):
self.name = name
self.stu_list = []
self.stu_dict = {}
def add_stu(self, stu):
self.stu_list.append(stu)
self.stu_dict[stu.num] = stu
def del_stu(self, num):
s = self.stu_dict.pop(num)
self.stu_list.remove(s)
def sort_stu(self, key=None, reverse=False):
self.stu_list.sort(key=key, reverse=reverse)
def get_stu(self, num):
return self.stu_dict.get(num)
def show_stu(self):
for s in self.stu_list:
print(s)
from random import randint
c = Class('ZZ-JY-PY1805')
for i in range(10):
name = 'stu' + str(i+1)
num = 100 + i
age = randint(20, 30)
score = randint(0, 100)
stu = Student(name, num, age, score)
c.add_stu(stu)
c.sort_stu(key=lambda s: s.score, reverse=True)
c.show_stu()
s = c.get_stu(104)
print(s)