赛季累计数据对比实用脚本
以下是几个实用场景的脚本示例,覆盖常见的数据对比需求:

Python版:通用赛季数据对比
#!/usr/bin/env python3
"""
赛季累计数据对比工具
支持多赛季数据对比、可视化展示
"""
import json
from datetime import datetime
from typing import Dict, List
class SeasonStats:
def __init__(self, season, data):
self.season = season
self.data = data # {"得分": 100, "篮板": 50, "助攻": 30}
def get_stat(self, key):
return self.data.get(key, 0)
def compare_seasons(seasons_data: List[SeasonStats]):
"""对比多个赛季的数据"""
result = {}
# 获取所有统计指标
all_keys = set()
for s in seasons_data:
all_keys.update(s.data.keys())
# 计算每个指标的变化
for key in all_keys:
values = [(s.season, s.get_stat(key)) for s in seasons_data]
if len(values) >= 2:
change = values[-1][1] - values[0][1]
change_pct = (change / values[0][1] * 100) if values[0][1] != 0 else 0
result[key] = {
"values": values,
"总变化": change,
"变化百分比": f"{change_pct:+.1f}%"
}
return result
# 示例数据
season_2023 = SeasonStats("2023-24", {"得分": 2000, "篮板": 800, "助攻": 500})
season_2024 = SeasonStats("2024-25", {"得分": 2300, "篮板": 850, "助攻": 550})
result = compare_seasons([season_2023, season_2024])
# 打印结果
for stat, info in result.items():
print(f"\n📊 {stat}:")
print(f" {' → '.join([f'{s}: {v}' for s, v in info['values']])}")
print(f" 变化: {info['总变化']:+d} ({info['变化百分比']})")
Bash脚本:简单快速对比
#!/bin/bash
# 赛季数据对比 - 简化版(适合快速查看)
# 定义赛季数据(可修改)
season1_name="2023-24"
season2_name="2024-25"
# 格式: 指标:赛季1值:赛季2值
declare -a stats=(
"得分:2000:2300"
"篮板:800:850"
"助攻:500:550"
"抢断:150:160"
"盖帽:80:75"
)
echo "📊 赛季数据对比 ($season1_name vs $season2_name)"
echo "================================================"
for stat in "${stats[@]}"; do
IFS=':' read -r name val1 val2 <<< "$stat"
diff=$((val2 - val1))
if [ $diff -gt 0 ]; then
change="▲ +$diff"
elif [ $diff -lt 0 ]; then
change="▼ $diff"
else
change="= 持平"
fi
printf "%-8s | %6s | %6s | %s\n" "$name" "$val1" "$val2" "$change"
done
使用SQL进行数据库对比
-- 如果你有数据库存储球员数据
WITH seasonal_totals AS (
SELECT
player_id,
season,
SUM(points) as total_points,
SUM(rebounds) as total_rebounds,
SUM(assists) as total_assists
FROM game_stats
GROUP BY player_id, season
)
SELECT
a.player_id,
a.season as current_season,
a.total_points as current_points,
b.total_points as previous_points,
(a.total_points - b.total_points) as point_diff,
ROUND((a.total_points - b.total_points) / b.total_points * 100, 1) as point_change_pct
FROM seasonal_totals a
JOIN seasonal_totals b
ON a.player_id = b.player_id
AND a.season > b.season
WHERE a.total_points > 0 AND b.total_points > 0
ORDER BY point_change_pct DESC;
生成可视化图表(Python + Matplotlib)
import matplotlib.pyplot as plt
import numpy as np
def plot_season_comparison(seasons, stats_data):
"""生成柱状图对比"""
seasons = ['2023-24', '2024-25']
stats = ['得分', '篮板', '助攻']
values = {
'2023-24': [2000, 800, 500],
'2024-25': [2300, 850, 550]
}
x = np.arange(len(stats))
width = 0.35
fig, ax = plt.subplots(figsize=(10, 6))
rects1 = ax.bar(x - width/2, values[seasons[0]], width, label=seasons[0])
rects2 = ax.bar(x + width/2, values[seasons[1]], width, label=seasons[1])
ax.set_xlabel('统计指标')
ax.set_ylabel('数值')
ax.set_title('赛季数据对比')
ax.set_xticks(x)
ax.set_xticklabels(stats)
ax.legend()
plt.tight_layout()
plt.show()
进阶:生成完整HTML报告
def generate_html_report(comparison_result):
"""生成HTML格式的对比报告"""
html = """
<html>
<head>
<style>
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px; text-align: left; border: 1px solid #ddd; }
th { background-color: #f2f2f2; }
.positive { color: green; }
.negative { color: red; }
</style>
</head>
<body>
<h1>赛季数据对比报告</h1>
"""
for stat, info in comparison_result.items():
html += f"""
<h2>{stat}</h2>
<table>
<tr><th>赛季</th><th>数值</th></tr>
"""
for season, value in info['values']:
html += f"<tr><td>{season}</td><td>{value}</td></tr>"
html += f"""
<tr><td><b>变化</b></td><td>{info['总变化']} ({info['变化百分比']})</td></tr>
</table>
"""
html += "</body></html>"
return html
# 使用
# with open('season_report.html', 'w') as f:
# f.write(generate_html_report(result))
使用建议
| 场景 | 推荐方案 |
|---|---|
| 快速查看 | Bash 脚本最简单 |
| 数据分析 | Python 更灵活可扩展 |
| 大量数据 | SQL 效率最高 |
| 汇报展示 | 生成图表或HTML报告 |
你需要哪个具体场景的实现?或者告诉我你的数据格式,我可以帮你定制更具体的脚本。