python案例统计克鲁伊夫转身做了几次?

wen python案例 4

本文目录导读:

python案例统计克鲁伊夫转身做了几次?

  1. 场景1:统计文本中出现次数(词频统计)
  2. 场景2:从比赛事件列表统计(结构化数据)
  3. 场景3:从CSV/Excel文件读取数据
  4. 场景4:如果是视频分析(更复杂,需要OpenCV/MediaPipe)

我可以帮你用Python来统计“克鲁伊夫转身”在文本中出现的次数,不过我需要先了解你的数据来源。

可能的场景:

  1. 你有比赛统计数据(传球、射门、过人次数)
  2. 你有文本(比赛报告、评论文章)需要分析
  3. 你有视频/运动捕捉数据

以下是几个常见场景的Python解决方案:


场景1:统计文本中出现次数(词频统计)

import re
from collections import Counter
# 示例文本
def count_cruyff_turn(text):
    # 匹配“克鲁伊夫转身”及其变体
    patterns = [
        r'克鲁伊夫转身',
        r'Cruyff\s+turn',
        r'克鲁伊夫式转身',
        r'克罗伊夫转身'  # 误写变体
    ]
    total = 0
    for pattern in patterns:
        matches = re.findall(pattern, text, re.IGNORECASE)
        total += len(matches)
        print(f"'{pattern}'出现{len(matches)}次")
    return total
# 测试
text = "比赛中,梅西完成了一次克鲁伊夫转身,随后又进行了一次克鲁伊夫转身..."
count = count_cruyff_turn(text)
print(f"总计:{count}次")
# 更高效的写法(使用Counter)
def count_fast(text):
    # 将文本拆分为词
    words = text.split()
    counter = Counter(words)
    return counter.get('克鲁伊夫转身', 0) + counter.get('cruyff_turn', 0)

场景2:从比赛事件列表统计(结构化数据)

# 假设你有一个比赛事件列表
events = [
    {'类型': '传球', '球员': '梅西', '时间': 12},
    {'类型': '克鲁伊夫转身', '球员': 'C罗', '时间': 23},
    {'类型': '射门', '球员': '梅西', '时间': 45},
    {'类型': '克鲁伊夫转身', '球员': '内马尔', '时间': 67},
    {'类型': '克鲁伊夫转身', '球员': '梅西', '时间': 78},
]
def count_events(events, action_type='克鲁伊夫转身'):
    count = 0
    for event in events:
        if event.get('类型') == action_type:
            count += 1
    return count
print(f"全场克鲁伊夫转身:{count_events(events)}次")
# 按球员统计
def count_by_player(events, action_type='克鲁伊夫转身'):
    from collections import defaultdict
    player_counts = defaultdict(int)
    for event in events:
        if event.get('类型') == action_type:
            player_counts[event['球员']] += 1
    return dict(player_counts)
print(count_by_player(events))
# 输出: {'C罗': 1, '内马尔': 1, '梅西': 1}

场景3:从CSV/Excel文件读取数据

import pandas as pd
# 假设从CSV读取
def analyze_csv(file_path):
    df = pd.read_csv(file_path)
    # 假设列名为 'action_type'
    if 'action_type' in df.columns:
        cruyff_turns = df[df['action_type'] == '克鲁伊夫转身']
        print(f"数据中克鲁伊夫转身次数:{len(cruyff_turns)}")
        # 按球员分组
        per_player = cruyff_turns.groupby('player').size()
        print("按球员统计:")
        print(per_player)
        return len(cruyff_turns)
    return 0
# 使用方法:analyze_csv('比赛数据.csv')

场景4:如果是视频分析(更复杂,需要OpenCV/MediaPipe)

# 伪代码示例(需要专业姿态估计模型)
"""
视频中检测克鲁伊夫转身的简化方法:
1. 识别球员姿态关键点
2. 检测转身动作:连续帧中身体方向变化超过180度,且带球动作
3. 技术较复杂,一般需要预训练模型
"""
def analyze_video(video_path):
    # 这通常需要使用深度学习模型
    # 例如使用MediaPipe姿态估计 + SVM分类器
    pass

告诉我你的具体情况,我可以提供更针对性的代码!

  • 你的数据是什么格式?(CSV、JSON、文本文件?)
  • 是要统计历史数据中的某个指标吗?
  • 还是实时的比赛分析?

抱歉,评论功能暂时关闭!