简体   繁体   English

在 Python 中计算 15^6 组合的 for 循环的替代方法

[英]Alternative to for loops for calculating 15^6 combinations in Python

Today, I have a nested for loop in python to calculate the value of all different combinations in a horse racing card consisting of six different races;今天,我在python中有一个嵌套的for循环来计算一张由六种不同种族组成的赛马卡中所有不同组合的值; ie six different arrays (of different lengths, but up to 15 items per array).即六个不同的数组(长度不同,但每个数组最多 15 个项目)。 It can be up to 11 390 625 combinations (15^6).它最多可以有 11 390 625 种组合 (15^6)。

For each horse in each race, I calculate a value (EV) which I want to multiply.对于每场比赛中的每匹马,我计算了一个我想要乘以的值 (EV)。

Array 1: 1A,1B,1C,1D,1E,1F阵列 1:1A、1B、1C、1D、1E、1F
Array 2: 2A,2B,2C,2D,2E,2F阵列 2:2A、2B、2C、2D、2E、2F
Array 3: 3A,3B,3C,3D,3E,3F阵列 3:3A、3B、3C、3D、3E、3F
Array 4: 4A,4B,4C,4D,4E,4F阵列 4:4A、4B、4C、4D、4E、4F
Array 5: 5A,5B,5C,5D,5E,5F阵列 5:5A、5B、5C、5D、5E、5F
Array 6: 6A,6B,6C,6D,6E,6F阵列 6:6A、6B、6C、6D、6E、6F

1A * 1B * 1C * 1D * 1E * 1F = X,XX 1A * 1B * 1C * 1D * 1E * 1F = X,XX
.... .... .... .... ... ... ………………………………
6A * 6B * 6C * 6D * 6E * 6F 0 X,XX 6A * 6B * 6C * 6D * 6E * 6F 0 X,XX

Doing four levels is OK.做四级就OK了。 It takes me about 3 minutes.我大约需要 3 分钟。 I have yet not been able to do six levels.我还没有能够做到六个级别。

I need help in creating a better way of doing this, and have no idea how to proceed.我需要帮助来创建更好的方法,但不知道如何继续。 Does numpy perhaps offer help here? numpy 可能在这里提供帮助吗? Pandas?熊猫? I've tried compiling the code with Cython, but it did not help much.我试过用 Cython 编译代码,但它没有多大帮助。

My function takes in a list containing the horses in numerical order and their EV.我的函数接受一个列表,其中包含按数字顺序排列的马和它们的 EV。 (Since horse starting numbers do not start with zero, I add 1 to the index). (因为马的起始数字不是以零开头,所以我在索引中加了 1)。 I iterate through all the different races, and save the output for the combination into a dataframe.我遍历所有不同的种族,并将组合的输出保存到数据帧中。

    def calculateCombos(horses_in_race_1,horses_in_race_2,horses_in_race_3,horses_in_race_4,horses_in_race_5,horses_in_race_6,totalCombinations, df):
        totalCombinations = 0
        for idx1, hr1_ev in enumerate(horses_in_race_1):
            hr1_no = idx1 + 1
            for idx2, hr2_ev in enumerate(horses_in_race_2):
                hr2_no = idx2 + 1
                for idx3, hr3_ev in enumerate(horses_in_race_3):
                    hr3_no_ = idx3 + 1
                    for idx4, hr4_ev in enumerate(horses_in_race_4):
                        hr4_no = idx4 + 1
                        for idx5, hr5_ev in enumerate(horses_in_race_5):
                            hr5_no = idx5 + 1
                            for idx6, hr6_ev in enumerate(horses_in_race_6):
                                hr6_no = idx6 + 1
                                totalCombinations = totalCombinations + 1
                                combinationEV = hr1_ev * hr2_ev * hr3_ev * hr4_ev * hr5_ev * hr6_ev
                                new_row = {'Race1':str(hr1_no),'Race2':str(hr2_no),'Race3':str(hr3_no),'Race4':str(hr4_no),'Race5':str(hr5_no),'Race6':str(hr6_no), 'EV':combinationEV}
                                df = appendCombinationToDF(df, new_row)
        return df   

Why don't you try this and see if you can run the function without any issues?你为什么不试试这个,看看你是否可以毫无问题地运行这个函数? This works on my laptop (I'm using PyCharm).这适用于我的笔记本电脑(我使用的是 PyCharm)。 If you can't run this, then I would say that you need a better PC perhaps.如果你不能运行它,那么我会说你可能需要一台更好的电脑。 I did not encounter any memory error.我没有遇到任何内存错误。

Assume that we have the following:假设我们有以下内容:

horses_in_race_1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
horses_in_race_2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
horses_in_race_3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
horses_in_race_4 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
horses_in_race_5 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
horses_in_race_6 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

I have re-written the function as follows - made a change in enumeration.我已按如下方式重新编写了该函数 - 对枚举进行了更改。 Also, not using df as I do not know what function this is - appendCombinationToDF另外,不使用 df 因为我不知道这是什么功能 - appendCombinationToDF

def calculateCombos(horses_in_race_1,horses_in_race_2,horses_in_race_3,horses_in_race_4,horses_in_race_5,horses_in_race_6):
    for idx1, hr1_ev in enumerate(horses_in_race_1, start = 1):
        for idx2, hr2_ev in enumerate(horses_in_race_2, start = 1):
            for idx3, hr3_ev in enumerate(horses_in_race_3, start = 1):
                for idx4, hr4_ev in enumerate(horses_in_race_4, start = 1):
                    for idx5, hr5_ev in enumerate(horses_in_race_5, start = 1):
                        for idx6, hr6_ev in enumerate(horses_in_race_6, start = 1):
                            combinationEV = hr1_ev * hr2_ev * hr3_ev * hr4_ev * hr5_ev * hr6_ev
                            new_row = {'Race1':str(idx1),'Race2':str(idx2),'Race3':str(idx3),'Race4':str(idx4),'Race5':str(idx5),'Race6':str(idx6), 'EV':combinationEV}
                            l.append(new_row)
                            #df = appendCombinationToDF(df, new_row)
l = [] # df = ...
calculateCombos(horses_in_race_1, horses_in_race_2, horses_in_race_3, horses_in_race_4, horses_in_race_5, horses_in_race_6)

Executing len(l) , I get:执行len(l) ,我得到:

11390625 # maximum combinations possible. This means that above function ran successfully and computation succeeded.

If the above can be executed, replace list l with df and see if function can execute without encountering memory error.如果上面可以执行,用df替换list l ,看看函数是否可以执行而不会遇到内存错误。 I was able to run the above in less than 20-30 seconds.我能够在不到 20-30 秒的时间内运行上述内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM