You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

15000球员数据集下高效筛选符合条件11人阵容的技术问询

高效筛选符合条件的足球阵容方案

我有一个包含15000个字典元素的足球球员列表,每个字典结构如下:

{
    'id': '123456',
    'name': 'Foo Bar',
    'position': 'GK',
    'club':  'Python FC',
    'league': 'Champions League',
    'country': 'Neverland'
}

需要组建一支11人球队,需满足指定阵型要求,示例阵型如下:

formation = [('ST', 2), ('LM', 1), ('RM', 1), ('CM', 2), ('LB', 1), ('RB', 1), ('CB', 2), ('GK', 1)]

筛选条件:

  • 符合指定阵型
  • 球员来自至少5个不同国家
  • 同一俱乐部的球员最多4人

已尝试的低效方法

方法1

直接生成所有11人排列组合后筛选:

from itertools import permutations
from collections import Counter

squads = permutations(my_list, 11)
match_countries = [squad for squad in squads if 
    len(Counter([player['country'] for player in squad])) >= 5
    and max(Counter([player['club'] for player in squad]).values()) <= 4
  ]  

问题:耗时极长,会生成大量不符合阵型的无效阵容。

方法2

先按位置分组,再用笛卡尔积生成符合阵型的阵容,之后筛选国家和俱乐部条件:
首先按位置分组:

list_goalkeepers = [player for player in my_list if player['position'] == 'GK']
list_strikers = [player for player in my_list if player['position'] == 'ST']
list_lm = [player for player in my_list if player['position'] == 'LM']
# 其他位置同理完成分组

生成符合阵型的阵容:

from itertools import product

squads = product(list_goalkeepers, 
                 list_strikers, list_strikers, 
                 list_lm, list_rm, 
                 list_cm, list_cm, 
                 list_lb, list_rb, 
                 list_cb, list_cb)

筛选符合条件的阵容:

from collections import Counter

match_countries = [squad for squad in squads 
  if len(Counter([player['country'] for player in squad])) >= 5
  and max(Counter([player['club'] for player in squad]).values()) <= 4 ]

问题:生成的阵容均符合阵型,但仍需遍历全部阵容进行筛选,效率依旧低下。

请问是否存在更高效的实现方法?

内容的提问来源于stack exchange,提问作者F43G4N

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 23:01:53