我如何在没有单个项目重复的情况下查看列表的所有组合?

发布于 2025-01-24 12:43:19 字数 1270 浏览 0 评论 0原文

我正在与16名参与者一起参加比赛,他们将分为4个团队,分别有4个球员。

我打算将每个播放器名称映射到他们的平均点,然后使用平均积分平衡团队。如果有一种比生成所有组合更好 /更容易做到这一点的方法,请告诉我。我知道这是一个包装问题,但是我确实希望能够在多组团队之间进行选择,因此我的目标是不仅返回一组。

获得所有可能的团队组合非常容易:

import itertools
playerList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
groups = list(itertools.combinations(playerList, 4))
print(f'Generating {len(groups)} teams...')

返回“生成1820个团队……”,这是16个选择4,并且可以预期。产生的团队看起来像这样:

[[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 3, 6], [1, 2, 3, 7], [1, 2, 3, 8], ...]

我的问题是我需要生成这些团队的所有可能组合。这将是1820选择4 = 455,660,782,395可能的组合,根据我的经验测试,这需要很长的时间来计算。那将返回这样的东西:

[[[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 3, 6], [1, 2, 3, 7]], [[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 3, 6], [1, 2, 3, 8], ...]

我正在考虑减少计算时间,我可以摆脱重复项。实际上,没有球员会参加多个球队,因此第一组中的每个团队重复1、2和3是没有意义的。我通常会使用类似的内容进行编码:

allTeams = list(itertools.combinations(groups, 4))
validTeams = []
for i in allTeams:
    if any(val in i[0] for val in i[1]):
        pass
    else:
        validTeams.append(i)

注意:我还没有在上面运行代码块,因为我的计算机无法超越第一行。

这篇文章归结为两个问题:

  1. 我如何在不重复球员的情况下生成这些团队的所有(1820选择4)组合?
  2. 这是解决我问题的最佳方法吗?通过另一种方法,我会更好吗?

我对编码是相对较新的,因此,如果我有任何重大误解,请告诉我。

I am running a tournament with 16 participants, who will be split into 4 teams of 4 players each.

I intend to map each player name to their average points and then balance teams using average points. If there is a better / easier way to do this than by generating all combinations, please let me know. I am aware that this is a binpacking problem, but I do want to be able to choose between multiple sets of teams, so my goal is not to return only one set.

It is pretty easy to get all possible team combinations:

import itertools
playerList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
groups = list(itertools.combinations(playerList, 4))
print(f'Generating {len(groups)} teams...')

Returns "Generating 1820 teams...", which is 16 choose 4, and is expected. The teams generated look like this:

[[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 3, 6], [1, 2, 3, 7], [1, 2, 3, 8], ...]

My problem is that I need to generate all possible combinations of these teams. This would be 1820 choose 4 = 455,660,782,395 possible combinations, which, from my experience testing, takes a very, very long time to compute. That would return something like this:

[[[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 3, 6], [1, 2, 3, 7]], [[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 3, 6], [1, 2, 3, 8], ...]

I am thinking in order to reduce the computation time, I could get rid of duplicates. Practically, no player is going to be on multiple teams, so it makes no sense to repeat 1, 2, and 3 for every team in the first set. I would normally code that using something like this:

allTeams = list(itertools.combinations(groups, 4))
validTeams = []
for i in allTeams:
    if any(val in i[0] for val in i[1]):
        pass
    else:
        validTeams.append(i)

NOTE: I have not run the code block above yet because my computer cannot get past the first line.

This post boils down to two questions:

  1. How can I generate all (1820 choose 4) combinations of these teams without repeating players?
  2. Is this the best way to approach my problem? Would I be better off with a different approach?

I am relatively new to coding so if there are any big misconceptions on my end, please let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文