足球红黄牌统计脚本
下面给你一个实用的脚本,可以统计比赛中哪队红黄牌更多,支持多种使用方式。

Python 脚本(推荐,灵活可扩展)
"""
足球红黄牌统计脚本
统计两队红黄牌数量并对比哪队更多
"""
def analyze_cards(team_a_name, team_b_name, cards):
"""
cards: 列表,每个元素为 (队伍, 牌型)
队伍: 'A' 或 'B'
牌型: 'yellow' 或 'red'
"""
stats = {
team_a_name: {'yellow': 0, 'red': 0},
team_b_name: {'yellow': 0, 'red': 0}
}
team_map = {'A': team_a_name, 'B': team_b_name}
for team, card in cards:
team_name = team_map.get(team)
if team_name and card in ('yellow', 'red'):
stats[team_name][card] += 1
# 输出统计
print("=" * 40)
print(f"{'队伍':<12}{'黄牌':<8}{'红牌':<8}{'总计':<8}")
print("-" * 40)
for name, s in stats.items():
total = s['yellow'] + s['red']
print(f"{name:<12}{s['yellow']:<8}{s['red']:<8}{total:<8}")
# 对比
print("=" * 40)
a = stats[team_a_name]
b = stats[team_b_name]
a_total = a['yellow'] + a['red']
b_total = b['yellow'] + b['red']
if a_total > b_total:
print(f"👉 {team_a_name} 牌更多(多 {a_total - b_total} 张)")
elif b_total > a_total:
print(f"👉 {team_b_name} 牌更多(多 {b_total - a_total} 张)")
else:
print("👉 两队牌数相同")
# 红黄牌分别对比明细
print("-" * 40)
for card_type, label in [('yellow', '黄牌'), ('red', '红牌')]:
if a[card_type] > b[card_type]:
print(f"{label}: {team_a_name} 更多 ({a[card_type]} vs {b[card_type]})")
elif b[card_type] > a[card_type]:
print(f"{label}: {team_b_name} 更多 ({b[card_type]} vs {a[card_type]})")
else:
print(f"{label}: 持平 ({a[card_type]})")
print("=" * 40)
return stats
# ===== 使用示例 =====
if __name__ == "__main__":
# 示例1:手动输入比赛数据
cards = [
('A', 'yellow'), ('A', 'yellow'), ('A', 'red'), # A队 2黄1红
('B', 'yellow'), ('B', 'yellow'), ('B', 'yellow'),# B队 3黄0红
]
analyze_cards("曼城", "利物浦", cards)
输出示例:
========================================
队伍 黄牌 红牌 总计
----------------------------------------
曼城 2 1 3
利物浦 3 0 3
========================================
👉 两队牌数相同
----------------------------------------
黄牌: 利物浦 更多 (3 vs 2)
红牌: 曼城 更多 (1 vs 0)
========================================
简单 Shell 一行命令(快速统计)
如果你有比赛事件文本文件 match.txt,每行格式如 A yellow:
# 统计黄牌 grep -c "yellow" match.txt # 分别统计 echo "A队黄牌: $(grep -c '^A yellow' match.txt)" echo "A队红牌: $(grep -c '^A red' match.txt)" echo "B队黄牌: $(grep -c '^B yellow' match.txt)" echo "B队红牌: $(grep -c '^B red' match.txt)"
从 CSV 读取(适合真实比赛数据)
import csv
def analyze_from_csv(filename, team_a="A", team_b="B"):
"""CSV格式: team,card (team=A/B, card=yellow/red)"""
cards = []
with open(filename, encoding='utf-8') as f:
reader = csv.DictReader(f)
for row in reader:
cards.append((row['team'].strip(), row['card'].strip().lower()))
analyze_cards(team_a, team_b, cards)
# 使用: analyze_from_csv("match_cards.csv", "主队", "客队")
快速使用说明
- 把脚本保存为
card_stats.py - 修改
__main__里的cards列表填入你的比赛数据 - 运行:
python card_stats.py
如果你想让我针对具体场景调整,请告诉我:
- 数据来源是什么?(手动输入 / CSV / Excel / 抓取网页 / API)
- 是否需要统计两黄变一红的规则?
- 需要处理同一球员多张牌的逻辑吗?
- 是否要输出到 Excel 或图表?
我可以据此帮你定制更精确的脚本。