本文目录导读:

我来为您提供一个统计角球数并判断哪边领先的Python案例,这个程序可以实时监控和统计足球比赛的角球数据。
基础版本:角球统计程序
class CornerKickCounter:
def __init__(self, home_team="主队", away_team="客队"):
"""初始化角球计数器"""
self.home_team = home_team
self.away_team = away_team
self.home_corners = 0
self.away_corners = 0
self.corner_log = [] # 记录角球事件
def add_corner(self, team):
"""添加角球记录"""
if team == "home":
self.home_corners += 1
team_name = self.home_team
elif team == "away":
self.away_corners += 1
team_name = self.away_team
else:
print("无效的队伍标识,请输入 'home' 或 'away'")
return
# 记录日志
log_entry = f"第{len(self.corner_log)+1}个角球: {team_name}"
self.corner_log.append(log_entry)
print(log_entry)
def get_leader(self):
"""判断哪边领先"""
if self.home_corners > self.away_corners:
return f"{self.home_team}领先"
elif self.away_corners > self.home_corners:
return f"{self.away_team}领先"
else:
return "双方持平"
def get_statistics(self):
"""获取完整统计数据"""
return {
"home_team": self.home_team,
"away_team": self.away_team,
"home_corners": self.home_corners,
"away_corners": self.away_corners,
"leader": self.get_leader(),
"corner_log": self.corner_log[:] # 返回副本
}
def reset(self):
"""重置计数"""
self.home_corners = 0
self.away_corners = 0
self.corner_log = []
print("计数器已重置")
def __str__(self):
"""打印当前状态"""
return f"比分: {self.home_team} {self.home_corners} - {self.away_corners} {self.away_team}"
# 使用示例
def basic_demo():
print("=== 基础角球统计 ===")
match = CornerKickCounter("曼联", "利物浦")
# 模拟比赛过程
match.add_corner("home") # 曼联获得角球
match.add_corner("away") # 利物浦获得角球
match.add_corner("home") # 曼联再获角球
match.add_corner("home") # 曼联继续获得
match.add_corner("away") # 利物浦扳回一个
print(f"\n当前状态: {match}")
print(f"领先情况: {match.get_leader()}")
print(f"详细统计: {match.get_statistics()}")
if __name__ == "__main__":
basic_demo()
进阶版本:实时交互系统
import time
from datetime import datetime
import json
class LiveCornerKickTracker:
"""增强版的角球跟踪系统"""
def __init__(self):
self.matches = {}
def create_match(self, match_id, home_team, away_team):
"""创建新比赛"""
self.matches[match_id] = {
"home_team": home_team,
"away_team": away_team,
"home_corners": 0,
"away_corners": 0,
"events": [],
"start_time": datetime.now(),
"status": "进行中"
}
print(f"比赛创建成功: {home_team} VS {away_team}")
return match_id
def add_corner_kick(self, match_id, team, minute):
"""添加角球事件"""
if match_id not in self.matches:
print("比赛不存在")
return
match = self.matches[match_id]
if team in ["home", "away"]:
match[f"{team}_corners"] += 1
# 记录事件
event = {
"minute": minute,
"team": match[f"{team}_team"],
"type": "角球",
"timestamp": datetime.now()
}
match["events"].append(event)
print(f"[{match['home_team']} VS {match['away_team']}] 第{minute}分钟, {event['team']}获得角球!")
else:
print("无效的团队标识")
def get_current_status(self, match_id):
"""获取比赛实时状态"""
if match_id not in self.matches:
return "比赛不存在"
match = self.matches[match_id]
hc = match["home_corners"]
ac = match["away_corners"]
# 判断领先
if hc > ac:
lead = f"{match['home_team']}领先{hc-ac}个角球"
elif ac > hc:
lead = f"{match['away_team']}领先{ac-hc}个角球"
else:
lead = "双方角球数持平"
status = {
"match_id": match_id,
"home_team": match["home_team"],
"away_team": match["away_team"],
"home_corners": hc,
"away_corners": ac,
"lead_info": lead,
"total_corners": hc + ac,
"status": match["status"]
}
return status
def display_match_info(self, match_id):
"""显示比赛信息"""
status = self.get_current_status(match_id)
if isinstance(status, dict):
print("\n" + "="*50)
print(f"比赛ID: {status['match_id']}")
print(f"比赛: {status['home_team']} VS {status['away_team']}")
print(f"比分: {status['home_corners']} - {status['away_corners']}")
print(f"领先情况: {status['lead_info']}")
print(f"总角球数: {status['total_corners']}")
print(f"比赛状态: {status['status']}")
print("="*50)
# 显示时间线
if match_id in self.matches and self.matches[match_id]["events"]:
print("\n时间线:")
for event in self.matches[match_id]["events"]:
print(f" {event['minute']}分钟: {event['team']} - {event['type']}")
def save_match_data(self, match_id, filename):
"""保存比赛数据到文件"""
if match_id not in self.matches:
print("比赛不存在")
return
with open(filename, 'w', encoding='utf-8') as f:
json.dump(self.matches[match_id], f, ensure_ascii=False, indent=2, default=str)
print(f"比赛数据已保存到 {filename}")
def load_match_data(self, filename):
"""从文件加载比赛数据"""
try:
with open(filename, 'r', encoding='utf-8') as f:
data = json.load(f)
# 重新加载到系统中
match_id = f"loaded_{datetime.now().timestamp()}"
self.matches[match_id] = data
print(f"比赛数据加载成功, 比赛ID: {match_id}")
# 显示加载的比赛信息
self.display_match_info(match_id)
return match_id
except FileNotFoundError:
print("文件不存在")
except json.JSONDecodeError:
print("无效的JSON格式")
# 进阶使用示例
def advanced_demo():
print("=== 进阶版角球追踪系统 ===")
tracker = LiveCornerKickTracker()
# 创建一场比赛
match_id = tracker.create_match("M001", "皇马", "巴萨")
# 模拟比赛过程
matches = [
(match_id, "home", 5),
(match_id, "away", 12),
(match_id, "home", 23),
(match_id, "away", 35),
(match_id, "home", 45),
(match_id, "home", 68),
(match_id, "away", 90)
]
for match, team, minute in matches:
tracker.add_corner_kick(match, team, minute)
time.sleep(0.5) # 模拟实时更新
# 显示比赛状态
tracker.display_match_info(match_id)
# 保存数据
tracker.save_match_data(match_id, "match_data.json")
# 取消注释以测试加载功能
# tracker.load_match_data("match_data.json")
# 分析功能版本
class CornerAnalysis:
"""角球数据分析"""
def __init__(self, tracker):
self.tracker = tracker
def analyze_trend(self, match_id):
"""分析角球趋势"""
if match_id not in self.tracker.matches:
return
match = self.tracker.matches[match_id]
events = match["events"]
if not events:
print("暂无角球数据")
return
# 按时间分段分析
periods = {
"上半场(0-45)": [e for e in events if e["minute"] <= 45],
"下半场(46-90)": [e for e in events if e["minute"] > 45]
}
print("\n=== 角球趋势分析 ===")
for period, period_events in periods.items():
home = sum(1 for e in period_events if e["team"] == match["home_team"])
away = sum(1 for e in period_events if e["team"] == match["away_team"])
print(f"{period}: 主队{home}个, 客队{away}个")
def get_efficiency_ratio(self, match_id):
"""计算角球效率比率"""
if match_id not in self.tracker.matches:
return None
match = self.tracker.matches[match_id]
hc = match["home_corners"]
ac = match["away_corners"]
total = hc + ac
if total == 0:
return 0
return {
"home_percentage": (hc / total) * 100,
"away_percentage": (ac / total) * 100,
"home_ratio": hc / ac if ac > 0 else float('inf'),
"away_ratio": ac / hc if hc > 0 else float('inf')
}
if __name__ == "__main__":
print("选择运行模式:")
print("1. 基础版")
print("2. 进阶版")
choice = input("请输入选项 (1/2): ")
if choice == "1":
basic_demo()
elif choice == "2":
advanced_demo()
else:
print("无效选项")
使用说明
- 基础版:简单记录角球数并判断领先方
- 进阶版:包含时间记录、数据分析、数据保存等功能
- 交互操作:
add_corner("home")或add_corner("away")添加角球get_leader()获取当前领先情况reset()重置计数器
这个系统可以用于:
- 替换人工记录角球
- 实时显示比赛数据
- 分析比赛趋势
- 数据存档和追踪
您可以根据需要选择适合的版本进行使用。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播