Python遍历JSON文件:计算指定套餐近3周期日均文档数平均值
实现
task_3函数计算指定套餐的日均文档数平均值 需求说明
实现task_3函数,返回套餐为['ENTERPRISE', 'FLEXIBLE']的记录在最近三个周期内的日均文档数平均值(整数类型),示例返回值为64。
约束条件
- 不允许修改函数体外部的代码;
- 不允许添加额外的导入语句;
- 数据周期可能存在缺失(如存在2022-01、2022-03但缺少2022-02),此时默认该缺失周期的文档数为0。
数据示例
import json data_in = [ { "package": "FLEXIBLE", "created": "2020-03-10T00:00:00", "summary": [ { "period": "2019-12", "documents": { "incomes": 63, "expenses": 13 } }, { "period": "2020-02", "documents": { "incomes": 45, "expenses": 81 } } ] }, { "package": "ENTERPRISE", "created": "2020-04-19T00:00:00", "summary": [ { "period": "2020-01", "documents": { "incomes": 15, "expenses": 52 } }, { "period": "2020-02", "documents": { "incomes": 76, "expenses": 47 } } ] }, { 'package': 'FLEXIBLE', 'created': '2020-01-15T00:00:00', 'summary': [ { 'period': '2020-03', 'documents': { 'incomes': 39, 'expenses': 48 } }, { 'period': '2020-04', 'documents': { 'incomes': 76, 'expenses': 20 } } ] }, { 'package': 'INTERNAL', 'created': '2020-01-07T00:00:00', 'summary': [ { 'period': '2019-12', 'documents': { 'incomes': 4, 'expenses': 53 } }, { 'period': '2020-01', 'documents': { 'incomes': 60, 'expenses': 48 } }, { 'period': '2020-02', 'documents': { 'incomes': 88, 'expenses': 85 } }, { 'period': '2020-03', 'documents': { 'incomes': 84, 'expenses': 81 } } ] } ]
完整实现代码
def task_3(data_in): ''' Return average(integer) number of documents per day in last three periods for package in ['ENTERPRISE', 'FLEXIBLE'] ex. 64 ''' target_packages = {'ENTERPRISE', 'FLEXIBLE'} # 存储所有周期的总文档数(多个记录同周期则累加) period_doc_counts = {} # 非闰年各月份天数 month_day_map = { '01':31, '02':28, '03':31, '04':30, '05':31, '06':30, '07':31, '08':31, '09':30, '10':31, '11':30, '12':31 } # 1. 收集所有目标套餐的周期文档数 for record in data_in: if record['package'] not in target_packages: continue for period_item in record['summary']: period = period_item['period'] total_docs = period_item['documents']['incomes'] + period_item['documents']['expenses'] if period in period_doc_counts: period_doc_counts[period] += total_docs else: period_doc_counts[period] = total_docs # 2. 确定全局最近的三个连续周期 if not period_doc_counts: return 0 # 按时间降序排序所有周期 sorted_periods = sorted(period_doc_counts.keys(), reverse=True) # 以最近的周期为基准,生成连续的三个周期 latest_year, latest_month = map(int, sorted_periods[0].split('-')) required_periods = [] for offset in range(3): curr_year = latest_year curr_month = latest_month - offset if curr_month < 1: curr_year -= 1 curr_month += 12 required_periods.append(f"{curr_year}-{curr_month:02d}") # 调整为从早到晚的顺序(不影响计算,仅逻辑清晰) required_periods = required_periods[::-1] # 3. 计算总文档数和总天数 total_docs = 0 total_days = 0 for period in required_periods: total_docs += period_doc_counts.get(period, 0) _, month = period.split('-') total_days += month_day_map[month] # 4. 计算日均平均值,返回整数(整数除法向下取整) return total_docs // total_days if total_days !=0 else 0
实现逻辑说明
- 筛选并收集数据:遍历输入数据,仅处理
ENTERPRISE和FLEXIBLE套餐的记录,累加每个周期的总文档数(收入文档+支出文档)。 - 确定目标周期:将所有收集到的周期按时间降序排序,以最近的周期为基准,生成连续的三个月份周期,确保覆盖缺失的周期。
- 处理缺失周期:对每个目标周期,若存在文档数则取实际值,否则按0计算。
- 计算平均值:统计三个周期的总文档数和总天数,用总文档数除以总天数,通过整数除法得到整数结果。
内容的提问来源于stack exchange,提问作者Marek Grzesiak
相关产品推荐
相关产品推荐

