输出的组合会导致大量内存需求

时间:2018-06-24 09:22:52

标签: python combinations

在工作中,我们席卷了世界杯,桌子开始分裂。我很感兴趣谁被完全排除在胜利之外,以及人们必须赢得多少结果。

有30个奇数参加。我决定在第一轮比赛中一路产生可能得分的矩阵,一直到5:5(因为没人得分超过4)。

list(itertools.product(range(5),repeat=2))

然后我将为每个参赛者创建一个矩阵以捕获结果...

   for i,guess in enumerate(PARTICIPANTS[0].predict): # number of matchies, data is specific guess
        for j,item in enumerate(match_possibilities[i]): # simulated outcomes
        #print(item,guess)
            if item == guess: # score match = 3pt
                PARTICIPANTS[0].sim_results[i][j] = 3
            elif item[0] > item[1] and guess[0] > guess[1]: #correct win
                PARTICIPANTS[0].sim_results[i][j] = 1
            elif item[0] < item[1] and guess[0] < guess[1]: #correct win
                PARTICIPANTS[0].sim_results[i][j] = 1
            elif item[0] == item[1] and guess[0] == guess[1]: #correct draw
                PARTICIPANTS[0].sim_results[i][j] = 1
            else: PARTICIPANTS[0].sim_results[i][j] = 0 # incorrect

这很好用...然后我尝试收集可能的总成绩时出现了问题

x = list(((itertools.product(*PARTICIPANTS[0].sim_results))))

太多的组合和类似的问题表明需要70多个演出RAM。

为简化此过程,与其检查所有得分结果,不如将其限制为一组并添加通用的胜利,失败,平局检查。 IF 一个人没有胜利或失败。 / p>

示例输出现在看起来像这样:

[[3, 1, 0, 0, 1, 1, 0, 0, 0, 0],
 [3, 1, 1, 1, 0, 0, 1, 0, 1],
 [3, 1, 1, 0, 0, 0, 0, 1],
 [3, 1, 1, 0, 1, 0, 1, 1],
 [1, 1, 1, 1, 1, 1, 3, 1],
 [0, 0, 1, 0, 0, 1, 0, 3],
 [3, 1, 1, 0, 0, 1, 1, 0, 1, 1],
 [1, 1, 0, 3, 0, 0, 0, 0, 0, 0],
 [3, 1, 1, 1, 0, 0, 0, 0],
 [0, 0, 1, 0, 0, 0, 0, 3, 0, 1],
 [1, 1, 0, 3, 0, 1, 0, 0, 0, 0],
 [1, 1, 0, 1, 0, 0, 0, 1, 3]]

每一行都是一个匹配项,每一列都是可能的结果。

问题是执行[ sum(x) for x in (itertools.product(*my_list)]仍然会导致需要大量RAM来存储结果。有没有一种方法可以根据所有剩余比赛结果以一致的方法来累积可能的分数,因此我可以对其他参与者执行相同的分数并比较分数。

0 个答案:

没有答案