解决方法:
以下是一个示例代码:
class Office:
def __init__(self, name, year):
self.name = name
self.year = year
class OfficeManager:
def __init__(self):
self.office_list = []
def add_office(self, office):
self.office_list.append(office)
def get_offices_by_year(self, year):
new_offices = [office for office in self.office_list if office.year == year]
return new_offices
# 创建 OfficeManager 实例
manager = OfficeManager()
# 添加办事处
manager.add_office(Office("办事处1", 2020))
manager.add_office(Office("办事处2", 2021))
manager.add_office(Office("办事处3", 2021))
manager.add_office(Office("办事处4", 2022))
# 按年份获取新办事处列表
new_offices = manager.get_offices_by_year(2021)
# 打印输出新办事处列表
for office in new_offices:
print(office.name)
运行以上代码将输出:
办事处2
办事处3
上一篇:按年查询Postgresql
下一篇:按年代划分的堆叠柱状图