实用脚本统计射门次数哪队更多?

wen 实用脚本 3

统计射门次数哪队更多 - 实用脚本

下面给你几个不同场景的实用脚本,可以统计哪队射门次数更多。

实用脚本统计射门次数哪队更多?


Python 处理 Excel/CSV 比赛数据

假设你的数据文件 shots.csv 结构如下:

team,shots
A队,12
B队,8
A队,15
B队,10

脚本 count_shots.py

import pandas as pd
from collections import Counter
def count_shots(file_path, team_col="team", shot_col="shots"):
    df = pd.read_csv(file_path)
    # 按队汇总射门次数
    total = df.groupby(team_col)[shot_col].sum().sort_values(ascending=False)
    print("=== 各队射门总数 ===")
    print(total.to_string())
    winner = total.idxmax()
    print(f"\n射门次数更多的队伍:{winner}({total.max()} 次)")
    return total
if __name__ == "__main__":
    count_shots("shots.csv")

运行结果示例:

=== 各队射门总数 ===
team
A队    27
B队    18
射门次数更多的队伍:A队(27 次)

统计单场比赛事件日志(含时间、队伍、事件类型)

数据 events.csv:

minute,team,event
12,A队,shot
23,B队,shot
45,A队,shot
67,B队,shot
78,A队,shot

脚本 match_shots.py

import csv
from collections import Counter
def count_match_shots(file_path):
    shots = Counter()
    with open(file_path, newline="", encoding="utf-8") as f:
        reader = csv.DictReader(f)
        for row in reader:
            if row["event"].strip().lower() == "shot":
                shots[row["team"]] += 1
    print("=== 本场射门统计 ===")
    for team, n in shots.most_common():
        print(f"{team}: {n} 次")
    if shots:
        winner, n = shots.most_common(1)[0]
        print(f"\n射门更多:{winner}({n} 次)")
if __name__ == "__main__":
    count_match_shots("events.csv")

从文本直播/解说中粗略统计(正则匹配)

如果只有一段文字直播稿,可以按队名关键词匹配"射门":

import re
from collections import Counter
def count_from_text(text, teams=("A队", "B队")):
    counter = Counter()
    for team in teams:
        # 匹配 "A队...射门" 这种近距离共现
        pattern = rf"{re.escape(team)}[^。!?\n]{{0,15}}射门"
        counter[team] = len(re.findall(pattern, text))
    return counter
if __name__ == "__main__":
    sample = """
    A队前锋禁区内射门被扑出。
    B队远射偏出。
    A队再次射门得分!
    """
    result = count_from_text(sample)
    print(result)
    print("射门更多:", result.most_common(1)[0][0])

⚠️ 文本统计仅作粗估,准确数据建议用结构化事件表。


命令行一行版(快速查看)

如果已有两列 team,shots 的 CSV:

awk -F, 'NR>1{s[$1]+=$2} END{for(t in s) print t, s[t]}' shots.csv | sort -k2 -nr

输出按射门数降序排列,第一行即射门更多的队。


使用建议

场景 推荐方案
有 Excel/CSV 结构化数据 场景一(pandas)
有逐事件日志 场景二(Counter)
只有文字直播 场景三(正则,粗估)
临时快速查看 场景四(awk)

需要我根据你实际的数据格式(比如列名、文件类型、是否多场比赛)定制脚本吗?把样例数据贴出来即可。

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