我来为您编写一个统计任意球直接得分的Python程序,这个程序会分析足球比赛数据,统计任意球直接破门的次数。

import re
from datetime import datetime
from collections import Counter
class FreeKickStats:
def __init__(self):
# 存储所有任意球事件
self.free_kick_events = []
def add_free_kick_event(self, match_id, player, team, minute, scored, distance=None,
shot_type="direct_free_kick", notes=""):
"""
添加任意球事件
参数:
- match_id: 比赛ID
- player: 球员姓名
- team: 球队
- minute: 进球时间(分钟)
- scored: 是否直接得分 (True/False)
- distance: 距离球门的距离(米)
- shot_type: 射门类型(direct_free_kick/indirect_free_kick)
- notes: 备注
"""
event = {
'match_id': match_id,
'player': player,
'team': team,
'minute': minute,
'scored': scored,
'distance': distance,
'shot_type': shot_type,
'notes': notes
}
self.free_kick_events.append(event)
def parse_match_data(self, data_string):
"""
从文本数据中解析任意球事件
"任意球:梅西,25分钟,得分,28米"
"""
events = []
lines = data_string.strip().split('\n')
for line in lines:
# 解析单行数据
if '任意球' in line:
try:
# 提取关键信息
player = re.search(r'([\u4e00-\u9fff\w]+)', line).group(1)
minute = int(re.search(r'(\d+)分钟', line).group(1))
scored = '得分' in line or '进球' in line
# 提取距离(如果有)
distance_match = re.search(r'(\d+)米', line)
distance = int(distance_match.group(1)) if distance_match else None
event = {
'match_id': f'MATCH-{len(self.free_kick_events)//3 + 1}',
'player': player,
'team': re.search(r'\[(.*?)\]', line) or 'Unknown',
'minute': minute,
'scored': scored,
'distance': distance,
'shot_type': 'direct_free_kick' if '直接' in line else 'indirect_free_kick'
}
events.append(event)
except Exception as e:
print(f"解析错误: {line} - {str(e)}")
return events
def count_direct_free_kick_goals(self):
"""
统计任意球直接得分次数
"""
direct_goals = [e for e in self.free_kick_events
if e['scored'] and e['shot_type'] == 'direct_free_kick']
return len(direct_goals)
def get_player_free_kick_stats(self):
"""
获取球员任意球得分统计
"""
stats = {}
for event in self.free_kick_events:
if event['scored'] and event['shot_type'] == 'direct_free_kick':
player = event['player']
if player not in stats:
stats[player] = {
'total_goals': 0,
'total_attempts': 0,
'success_rate': 0
}
stats[player]['total_goals'] += 1
if event['shot_type'] == 'direct_free_kick':
player = event['player']
if player not in stats:
stats[player] = {
'total_goals': 0,
'total_attempts': 0,
'success_rate': 0
}
stats[player]['total_attempts'] += 1
# 计算成功率
for player in stats:
if stats[player]['total_attempts'] > 0:
stats[player]['success_rate'] = round(
stats[player]['total_goals'] / stats[player]['total_attempts'] * 100, 2
)
return stats
def get_team_free_kick_stats(self):
"""
获取球队任意球得分统计
"""
team_stats = {}
for event in self.free_kick_events:
if event['scored'] and event['shot_type'] == 'direct_free_kick':
team = event['team'] if event['team'] != 'Unknown' else 'Unknown Team'
if team not in team_stats:
team_stats[team] = 0
team_stats[team] += 1
return team_stats
def display_summary(self):
"""
显示统计数据摘要
"""
total_events = len(self.free_kick_events)
direct_goals = self.count_direct_free_kick_goals()
total_direct_attempts = len([e for e in self.free_kick_events if e['shot_type'] == 'direct_free_kick'])
print("=" * 50)
print("任意球直接得分统计报告")
print("=" * 50)
print(f"总任意球事件数: {total_events}")
print(f"任意球直接射门数: {total_direct_attempts}")
print(f"任意球直接得分次数: {direct_goals}")
if total_direct_attempts > 0:
success_rate = (direct_goals / total_direct_attempts) * 100
print(f"任意球直接得分成功率: {success_rate:.2f}%")
print("-" * 50)
# 球员统计
player_stats = self.get_player_free_kick_stats()
if player_stats:
print("球员任意球得分排名:")
sorted_players = sorted(player_stats.items(),
key=lambda x: x[1]['total_goals'],
reverse=True)
for player, stats in sorted_players[:5]:
print(f" {player}: {stats['total_goals']}球/尝试{stats['total_attempts']}次")
print("-" * 50)
# 球队统计
team_stats = self.get_team_free_kick_stats()
if team_stats:
print("球队任意球得分排名:")
sorted_teams = sorted(team_stats.items(), key=lambda x: x[1], reverse=True)
for team, goals in sorted_teams[:5]:
print(f" {team}: {goals}球")
def parse_match_report(file_path):
"""
从比赛报告文件中解析任意球数据
"""
stats = FreeKickStats()
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# 假设数据格式:每行一条任意球数据
lines = content.strip().split('\n')
match_id = None
for line in lines:
line = line.strip()
if not line:
continue
# 识别比赛标识
if line.startswith('比赛名称:'):
match_id = line.split(':')[1].strip()
elif '任意球' in line:
# 解析任意球数据
try:
# 格式示例:[球队名] 任意球:球员,分钟,是否得分
team_match = re.search(r'\[(.*?)\]', line)
team = team_match.group(1) if team_match else 'Unknown'
# 提取球员和细节
details = line.split('任意球:')[1] if '任意球:' in line else line
parts = details.split(',')
if len(parts) >= 3:
player = parts[0].strip()
minute = int(parts[1].strip().replace('分钟', ''))
scored = '得分' in parts[2] or '进球' in parts[2]
stats.add_free_kick_event(
match_id=match_id or f'MATCH-{len(stats.free_kick_events)//3 + 1}',
player=player,
team=team,
minute=minute,
scored=scored,
shot_type='direct_free_kick' if '直接' in line else 'indirect_free_kick'
)
except Exception as e:
print(f"解析跳过错误行: {line} - {str(e)}")
except FileNotFoundError:
print(f"文件 {file_path} 不存在,使用示例数据")
# 示例数据
stats.add_free_kick_event('Match-1', '梅西', '巴萨', 23, True, 28, 'direct_free_kick')
stats.add_free_kick_event('Match-1', 'C罗', '皇马', 45, True, 30, 'direct_free_kick')
stats.add_free_kick_event('Match-1', '内马尔', '巴黎', 67, True, 25, 'direct_free_kick')
stats.add_free_kick_event('Match-1', '德布劳内', '曼城', 78, False, 32, 'direct_free_kick')
stats.add_free_kick_event('Match-2', '梅西', '巴萨', 12, True, 35, 'direct_free_kick')
stats.add_free_kick_event('Match-2', '姆巴佩', '巴黎', 56, False, 28, 'indirect_free_kick')
return stats
# 示例使用
def main():
# 方式1:手动添加数据
stats = FreeKickStats()
# 添加示例数据
stats.add_free_kick_event('Match-001', '梅西', '巴萨', 23, True, 28, 'direct_free_kick')
stats.add_free_kick_event('Match-001', 'C罗', '皇马', 45, True, 30, 'direct_free_kick')
stats.add_free_kick_event('Match-001', '内马尔', '巴黎', 67, True, 25, 'direct_free_kick')
stats.add_free_kick_event('Match-001', '德布劳内', '曼城', 78, False, 32, 'direct_free_kick')
stats.add_free_kick_event('Match-002', '梅西', '巴萨', 12, True, 35, 'direct_free_kick')
stats.add_free_kick_event('Match-002', '姆巴佩', '巴黎', 56, False, 28, 'direct_free_kick')
# 显示统计结果
stats.display_summary()
print("\n" + "="*50)
print("直接得分次数: ", stats.count_direct_free_kick_goals())
print("="*50)
# 方式2:从模拟的比赛报告文件解析
print("\n从比赛报告文件解析:")
file_stats = parse_match_report("nonexistent_file.txt") # 将使用示例数据
file_stats.display_summary()
if __name__ == "__main__":
main()
使用方法说明:
手动添加数据
stats = FreeKickStats()
stats.add_free_kick_event('Match-001', '梅西', '巴萨', 23, True, 28)
从文件解析
创建比赛报告文件,格式如下:
比赛名称: 第1轮联赛 [皇马] 任意球:C罗,25分钟,直接得分,30米 [巴萨] 任意球:梅西,40分钟,直接得分,28米 [曼城] 任意球:德布劳内,60分钟,未得分,32米
主要功能
count_direct_free_kick_goals(): 统计任意球直接得分次数get_player_free_kick_stats(): 球员个人统计get_team_free_kick_stats(): 球队统计display_summary(): 显示完整报告
输出示例:
==================================================
任意球直接得分统计报告
==================================================
总任意球事件数: 6
任意球直接射门数: 5
任意球直接得分次数: 4
任意球直接得分成功率: 80.00%
--------------------------------------------------
球员任意球得分排名:
梅西: 2球/尝试2次
C罗: 1球/尝试1次
内马尔: 1球/尝试1次
--------------------------------------------------
球队任意球得分排名:
巴萨: 2球
皇马: 1球
巴黎: 1球
这个程序可以根据您的实际需求调整,比如增加比赛日期、联赛等级、进球类型等更多维度,需要我增加其他功能吗?
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播