如何计算 python/itertools 中一级方程式比赛的所有组合

How to calculate all combinations of a Formula 1 race in python/itertools

我想做一个冠军calculator/simulator来帮助弄清楚车手如何赢得冠军的不同组合。

在仔细研究之后,我认为我需要使用 itertools。以下是我到目前为止所拥有的。问题是,目前它正在为每个车手生成每个位置,而我只需要它为每个组合为每个车手分配 1 个位置。

def Sim():
    drivers = ["Lewis Hamilton","Valteri Bottas","Max Verstappen","Sergio Perez","Lando Norris","Daniel Riccardo","Carlos Sainz","Charles Lecerc","Pierre Gasly","Yuki Tsunoda","Kimi Raikonnen","Antonio Giovnazzi","Sebastian Vettel","Lance Stroll","Fernando Alonso","Estaban Ocon","George Russel","Nicholas Latifi","Mick Schumacher","Nikita Mazepin"]
    pos = ["20", "19", "18", "17", "16", "15", "14", "13", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1"]
    c = list(itertools.product(drivers, pos))
    print(c)

我不知道我是否理解正确,但我认为您可能想要为驱动程序生成排列;如果我错了请纠正我,但是例如,对于 3 个驱动程序 A、B 和 C,它将是:

[(A, 1), (B, 2), (C, 3)]
[(A, 1), (B, 3), (C, 2)]
[(A, 2), (B, 1), (C, 3)]
[(A, 2), (B, 3), (C, 1)]
[(A, 3), (B, 1), (C, 2)]
[(A, 3), (B, 2), (C, 1)]

然后请注意,在您的情况下,排列数为 n! 20! ~ 2 * 10^18

所有可能排列的数量是巨大的,但你可以这样得到它们:

import itertools
drivers = ["Lewis Hamilton","Valteri Bottas","Max Verstappen","Sergio Perez","Lando Norris","Daniel Riccardo","Carlos Sainz","Charles Lecerc","Pierre Gasly","Yuki Tsunoda","Kimi Raikonnen","Antonio Giovnazzi","Sebastian Vettel","Lance Stroll","Fernando Alonso","Estaban Ocon","George Russel","Nicholas Latifi","Mick Schumacher","Nikita Mazepin"]
pos = list(range(len(drivers)))

p = itertools.permutations(pos)

for i in range(10):
    x = list(zip(drivers, next(iter(p))))
    print(x)

您看到的是排列(不是乘积)。但是20辆车可以完成2,432,902,008,176,640,000个位置排列。那是每个获胜车手 121,645,100,408,832,000。

即使你使用了正确的函数(排列),你也无法在超级计算机上处​​理它们,更不用说将它们存储在列表中了。

drivers = ["Lewis Hamilton","Valteri Bottas","Max Verstappen","Sergio Perez","Lando Norris","Daniel Riccardo","Carlos Sainz","Charles Lecerc","Pierre Gasly","Yuki Tsunoda","Kimi Raikonnen","Antonio Giovnazzi","Sebastian Vettel","Lance Stroll","Fernando Alonso","Estaban Ocon","George Russel","Nicholas Latifi","Mick Schumacher","Nikita Mazepin"]

from itertools import permutations

for raceResult in permutations(drivers):
    print(*(f"{p}:{d}" for p,d in enumerate(raceResult[:3],1)),end=" ... ")
    print(*(f"{p}:{d}" for p,d in enumerate(raceResult[18:],18)))

1:Lewis Hamilton 2:Valteri Bottas 3:Max Verstappen ... 18:Mick Schumacher 19:Nikita Mazepin
1:Lewis Hamilton 2:Valteri Bottas 3:Max Verstappen ... 18:Nikita Mazepin 19:Mick Schumacher
1:Lewis Hamilton 2:Valteri Bottas 3:Max Verstappen ... 18:Nicholas Latifi 19:Nikita Mazepin
1:Lewis Hamilton 2:Valteri Bottas 3:Max Verstappen ... 18:Nikita Mazepin 19:Nicholas Latifi
1:Lewis Hamilton 2:Valteri Bottas 3:Max Verstappen ... 18:Nicholas Latifi 19:Mick Schumacher
...

更容易管理的是只看前 3 个的排列(你的 19 个驱动程序只有 6,840 个):

for raceResult in permutations(drivers,3):
    print(*(f"{p}:{d}" for p,d in enumerate(raceResult,1)))

1:Lewis Hamilton 2:Valteri Bottas 3:Max Verstappen
1:Lewis Hamilton 2:Valteri Bottas 3:Sergio Perez
1:Lewis Hamilton 2:Valteri Bottas 3:Lando Norris
1:Lewis Hamilton 2:Valteri Bottas 3:Daniel Riccardo
1:Lewis Hamilton 2:Valteri Bottas 3:Carlos Sainz
1:Lewis Hamilton 2:Valteri Bottas 3:Charles Lecerc
1:Lewis Hamilton 2:Valteri Bottas 3:Pierre Gasly
1:Lewis Hamilton 2:Valteri Bottas 3:Yuki Tsunoda
1:Lewis Hamilton 2:Valteri Bottas 3:Kimi Raikonnen
1:Lewis Hamilton 2:Valteri Bottas 3:Antonio Giovnazzi
...