本文目录导读:

我来为你创建一个足球比赛中快发任意球尝试次数的统计案例。
完整案例代码
import random
from datetime import datetime
from collections import Counter
class QuickFreeKickStats:
"""快发任意球统计系统"""
def __init__(self):
self.attempts = [] # 存储所有尝试记录
self.players = {} # 球员数据
def record_attempt(self, player_name, team, minute, is_successful,
reason=None, opponent_action=None):
"""
记录一次快发任意球尝试
参数:
player_name: 球员名字
team: 球队名
minute: 比赛分钟
is_successful: 是否成功
reason: 失败原因
opponent_action: 对方动作
"""
attempt = {
'player': player_name,
'team': team,
'minute': minute,
'successful': is_successful,
'reason': reason,
'opponent_action': opponent_action,
'timestamp': datetime.now()
}
self.attempts.append(attempt)
# 更新球员统计
if player_name not in self.players:
self.players[player_name] = {'attempts': 0, 'successful': 0}
self.players[player_name]['attempts'] += 1
if is_successful:
self.players[player_name]['successful'] += 1
return attempt
def get_total_attempts(self):
"""获取总尝试次数"""
return len(self.attempts)
def get_success_rate(self):
"""获取成功率"""
if not self.attempts:
return 0.0
successful = sum(1 for a in self.attempts if a['successful'])
return successful / len(self.attempts) * 100
def get_team_stats(self):
"""获取球队统计"""
team_stats = {}
for attempt in self.attempts:
team = attempt['team']
if team not in team_stats:
team_stats[team] = {'attempts': 0, 'successful': 0}
team_stats[team]['attempts'] += 1
if attempt['successful']:
team_stats[team]['successful'] += 1
return team_stats
def get_failure_reasons(self):
"""获取失败原因统计"""
reasons = Counter()
for attempt in self.attempts:
if not attempt['successful'] and attempt['reason']:
reasons[attempt['reason']] += 1
return reasons
def get_opponent_actions(self):
"""获取对方干扰动作统计"""
actions = Counter()
for attempt in self.attempts:
if attempt['opponent_action']:
actions[attempt['opponent_action']] += 1
return actions
def simulate_match_quick_kicks(team1, team2, minutes=90):
"""模拟一场比赛中的快发任意球情况"""
stats = QuickFreeKickStats()
# 常见球员名字
players1 = [f"{team1}_球员{i}" for i in range(1, 12)]
players2 = [f"{team2}_球员{i}" for i in range(1, 12)]
# 快发任意球的一些常见情况
fail_reasons = [
"裁判阻止",
"未等裁判鸣哨",
"球未放稳",
"被对方抢断",
"传球失误",
"被对方干扰"
]
opponent_actions = [
"站在球前阻挡",
"上前封堵",
"用手阻挡",
"言语干扰",
"身体接触"
]
print(f"\n===== {team1} vs {team2} 快发任意球统计 =====")
print(f"比赛时间:{minutes}分钟")
print("-" * 40)
# 模拟快发任意球机会
total_chances = random.randint(3, 8)
print(f"全场比赛共有 {total_chances} 次快发任意球尝试机会")
print()
for i in range(total_chances):
minute = random.randint(1, minutes)
team = random.choice([team1, team2])
player = random.choice(players1 if team == team1 else players2)
# 70%概率成功,30%失败
is_successful = random.random() < 0.7
reason = None
opponent_action = None
if not is_successful:
reason = random.choice(fail_reasons)
if random.random() < 0.8: # 80%概率有对方干扰
opponent_action = random.choice(opponent_actions)
stats.record_attempt(
player_name=player,
team=team,
minute=minute,
is_successful=is_successful,
reason=reason,
opponent_action=opponent_action
)
# 输出每次尝试
status = "✅ 成功" if is_successful else "❌ 失败"
print(f"{i+1}. 第{minute}分钟 | {team} | {player} | {status}")
if reason:
print(f" 原因:{reason}")
if opponent_action:
print(f" 对方动作:{opponent_action}")
return stats
def analyze_and_display_stats(stats):
"""分析并显示统计数据"""
print("\n" + "="*50)
print("📊 数据分析报告")
print("="*50)
# 总统计
print(f"\n📍 总尝试次数:{stats.get_total_attempts()}")
print(f"📍 成功率:{stats.get_success_rate():.1f}%")
# 球队统计
print("\n🏟️ 球队统计:")
team_stats = stats.get_team_stats()
for team, team_data in team_stats.items():
success_rate = team_data['successful'] / team_data['attempts'] * 100 if team_data['attempts'] > 0 else 0
print(f" {team}: {team_data['attempts']}次尝试, {team_data['successful']}次成功, 成功率{success_rate:.1f}%")
# 失败原因分析
reasons = stats.get_failure_reasons()
if reasons:
print("\n📋 失败原因分析:")
for reason, count in reasons.most_common():
print(f" {reason}: {count}次")
# 对方干扰分析
actions = stats.get_opponent_actions()
if actions:
print("\n⚡ 对方干扰方式:")
for action, count in actions.most_common():
print(f" {action}: {count}次")
# 成功快发分析
successful_attempts = [a for a in stats.attempts if a['successful']]
if successful_attempts:
print("\n🏆 成功案例时间点:")
for attempt in successful_attempts:
print(f" 第{attempt['minute']}分钟 - {attempt['player']} ({attempt['team']})")
def main():
"""主函数"""
print("⚽ 快发任意球统计系统 ⚽")
print("="*30)
# 可以运行多场比赛模拟
total_matches = random.randint(2, 5)
all_stats = []
for match_num in range(1, total_matches + 1):
print(f"\n===== 第 {match_num} 场比赛 =====")
# 随机生成球队名
team1 = f"球队{random.randint(1, 20)}"
team2 = f"球队{random.randint(1, 20)}"
# 确保两队不同
while team1 == team2:
team2 = f"球队{random.randint(1, 20)}"
# 模拟比赛
match_stats = simulate_match_quick_kicks(team1, team2)
all_stats.append(match_stats)
# 分析每场比赛
analyze_and_display_stats(match_stats)
# 综合所有比赛
print("\n" + "="*50)
print("📈 综合统计")
print("="*50)
total_attempts = sum(stats.get_total_attempts() for stats in all_stats)
total_success = sum(
sum(1 for a in stats.attempts if a['successful'])
for stats in all_stats
)
print(f"总比赛场次:{total_matches}")
print(f"总快发任意球尝试:{total_attempts}次")
print(f"成功次数:{total_success}次")
print(f"总成功率:{total_success/total_attempts*100:.1f}%" if total_attempts > 0 else "无数据")
if __name__ == "__main__":
main()
运行示例
⚽ 快发任意球统计系统 ⚽ ============================== ===== 第 1 场比赛 ===== ===== 球队7 vs 球队15 快发任意球统计 ===== 比赛时间:90分钟 ---------------------------------------- 全场比赛共有 5 次快发任意球尝试机会 1. 第23分钟 | 球队15 | 球队15_球员8 | ✅ 成功 2. 第45分钟 | 球队7 | 球队7_球员3 | ❌ 失败 原因:被对方抢断 对方动作:身体接触 3. 第67分钟 | 球队15 | 球队15_球员5 | ✅ 成功 4. 第72分钟 | 球队7 | 球队7_球员11 | ❌ 失败 原因:裁判阻止 对方动作:站在球前阻挡 5. 第85分钟 | 球队15 | 球队15_球员2 | ✅ 成功 ================================================== 📊 数据分析报告 ================================================== 📍 总尝试次数:5 📍 成功率:60.0% 🏟️ 球队统计: 球队15: 3次尝试, 3次成功, 成功率100.0% 球队7: 2次尝试, 0次成功, 成功率0.0% 📋 失败原因分析: 被对方抢断: 1次 裁判阻止: 1次 ⚡ 对方干扰方式: 身体接触: 1次 站在球前阻挡: 1次 🏆 成功案例时间点: 第23分钟 - 球队15_球员8 (球队15) 第67分钟 - 球队15_球员5 (球队15) 第85分钟 - 球队15_球员2 (球队15)
代码说明
-
QuickFreeKickStats类:核心统计类
- 记录每次尝试的详细信息
- 提供各种统计分析方法
-
主要功能:
- 记录尝试次数和成功率
- 按球队和球员分类统计
- 分析失败原因
- 统计对方干扰方式
- 显示成功案例的时间点
-
模拟功能:
- 自动生成比赛数据
- 模拟多种快发任意球情况
- 随机生成比赛事件
这个系统可以帮助分析快发任意球的成功率、失败原因等关键数据,对研究战术很有帮助,你可以根据实际需要修改参数或添加更多功能。