2015-2020年在线教育投资事件数据分析 - 统计年度融资次数和金额
import json
读取数据
with open('data.json', 'r') as f: data = json.load(f)
定义函数,用于将金额转换为人民币
def convert_currency(amount, currency): if currency == '美元': return amount * 6.5 elif currency == '人民币': return amount else: return 0
输入需要统计的年份
year = input('请输入需要统计的年份:')
统计每个公司融资总次数和总金额
result = {} for d in data: if d['时间'].startswith(year): company = d['公司名称'] if company in result: result[company]['次数'] += 1 result[company]['金额'] += convert_currency(int(d['金额']), d['金额'][-2:]) else: result[company] = {'次数': 1, '金额': convert_currency(int(d['金额']), d['金额'][-2:])}
对结果进行排序
sorted_result = sorted(result.items(), key=lambda x: (-x[1]['次数'], -x[1]['金额'], x[0]))
将结果写入文件
with open('result.txt', 'w') as f: for r in sorted_result: f.write('{} {} {} '.format(r[0], r[1]['次数'], r[1]['金额']))
原文地址: https://www.cveoy.top/t/topic/oiYs 著作权归作者所有。请勿转载和采集!