dir_date1 = {
'三酷猫': {
'鲫鱼': [17, 10.5],
'鲤鱼': [8, 6.2],
'鲢鱼': [7, 4.7]
},
'加菲猫': {
'黑鱼': [8, 16]
},
'大脸猫': {
'草鱼': [12, 8]
}
}
dir_date2 = {
'三酷猫': {
'草鱼': [2, 7.2],
'鲫鱼': [3, 12],
'黑鱼': [6, 15]
},
'加菲猫': {
'鲤鱼': [9, 7.1]
}
}
dir_date3 = {
'三酷猫': {
'乌龟': [1, 78.10],
'鲫鱼': [1, 10.78],
'草鱼': [5, 7.92]
},
'大脸猫': {
'鲫鱼': [8, 9.8],
'螃蟹': [5, 15]
}
}
fish_records = {
'1月1日': dir_date1,
'1月2日': dir_date2,
'1月3日': dir_date3
}
# 钓鱼总数量
total_nums = 0
# 钓鱼总金额
total_amount = 0
# 三酷猫的统计结果
three_cool_cat_statistics = {}
for day, day_record in fish_records.items():
# 三酷猫当天钓鱼数量
day_nums = 0
# 三酷猫当天钓鱼金额
day_amount = 0
# 三酷猫当天每种鱼的数量和金额
daily_fish_statistics = {}
if '三酷猫' in day_record:
three_cool_cat_record = day_record['三酷猫']
for fish, fish_record in three_cool_cat_record.items():
# 统计每种鱼的数量和金额
if fish in daily_fish_statistics:
daily_fish_statistics[fish][0] += fish_record[0]
daily_fish_statistics[fish][1] += fish_record[0] * fish_record[1]
else:
daily_fish_statistics[fish] = [fish_record[0], fish_record[0] * fish_record[1]]
# 统计三酷猫当天钓鱼总数量和总金额
day_nums += fish_record[0]
day_amount += fish_record[0] * fish_record[1]
# 统计三天总的钓鱼数量和金额
total_nums += fish_record[0]
total_amount += fish_record[0] * fish_record[1]
# 保存三酷猫每天的统计结果
three_cool_cat_statistics[day] = {'数量': day_nums, '金额': day_amount, '鱼类统计': daily_fish_statistics}
print('=========每日钓鱼记录==========')
for day, stats in three_cool_cat_statistics.items():
# print(f'{day}钓鱼数量为{stats["数量"]}, 金额为{stats["金额"]}')
# print('鱼类统计:')
print(f'{day}钓鱼记录为:')
for fish, fish_stats in stats['鱼类统计'].items():
print(f'\t{fish}数量{fish_stats[0]},单价{fish_stats[1] / fish_stats[0]:.2f}元')
# 按鱼进行数量,金额统计
# 相同的鱼归为一类
# 合并每种鱼的统计结果
merged_fish_statistics = {}
for day, stats in three_cool_cat_statistics.items():
for fish, fish_stats in stats['鱼类统计'].items():
# 如果已经存在的鱼类只需要计算数量、金额
if fish in merged_fish_statistics:
# 计算每种鱼的总数量
merged_fish_statistics[fish][0] += fish_stats[0]
# 计算每种鱼的总金额
merged_fish_statistics[fish][1] += fish_stats[1]
# 不存在的鱼要添加
else:
merged_fish_statistics[fish] = [fish_stats[0], fish_stats[1]]
print('=====按鱼进行数量,金额统计=====')
for fish, fish_stats in merged_fish_statistics.items():
print(f'{fish}的总数量{fish_stats[0]},金额为{fish_stats[1]:.2f}元')
# 最大值,总数量,总金额统计
print('====最大值,总数量,总金额统计====')
max_fish_nums = max(merged_fish_statistics, key=lambda x: merged_fish_statistics[x][0])
max_fish_money = max(merged_fish_statistics, key=lambda y: merged_fish_statistics[y][1])
print(f'最大数量的鱼是{max_fish_nums},{merged_fish_statistics[max_fish_nums][0]}条')
print(f'最大金额的鱼是{max_fish_money},{merged_fish_statistics[max_fish_money][1]}元')
print(f'钓鱼总数量为{total_nums},总金额为{total_amount:.2f}元')
输出结果:
=========每日钓鱼记录==========
1月1日钓鱼记录为:
鲫鱼数量17,单价10.50元
鲤鱼数量8,单价6.20元
鲢鱼数量7,单价4.70元
1月2日钓鱼记录为:
草鱼数量2,单价7.20元
鲫鱼数量3,单价12.00元
黑鱼数量6,单价15.00元
1月3日钓鱼记录为:
乌龟数量1,单价78.10元
鲫鱼数量1,单价10.78元
草鱼数量5,单价7.92元
=====按鱼进行数量,金额统计=====
鲫鱼的总数量21,金额为225.28元
鲤鱼的总数量8,金额为49.60元
鲢鱼的总数量7,金额为32.90元
草鱼的总数量7,金额为54.00元
黑鱼的总数量6,金额为90.00元
乌龟的总数量1,金额为78.10元
====最大值,总数量,总金额统计====
最大数量的鱼是鲫鱼,21条
最大金额的鱼是鲫鱼,225.28元
钓鱼总数量为50,总金额为529.88元