今天,我在python中有一个嵌套的for循环,用于计算由六种不同比赛组成的赛马卡中所有不同组合的值;i、 e.六个不同的阵列(长度不同,但每个阵列最多15个项目)。它最多可以是11 390 625个组合(15^6)
对于每场比赛中的每匹马,我计算一个我想乘以的值(EV)
阵列1:1A、1B、1C、1D、1E、1F
阵列2:2A、2B、2C、2D、2E、2F
阵列3:3A、3B、3C、3D、3E、3F
阵列4:4A、4B、4C、4D、4E、4F
阵列5:5A、5B、5C、5D、5E、5F
数组6:6A、6B、6C、6D、6E、6F
1A*1B*1C*1D*1E*1F=X,XX
.... .... .... .... ... ...
6A*6B*6C*6D*6E*6F 0 X,XX
做四个级别就可以了。我花了大约3分钟。 我还没能完成六级
我需要帮助来创造一个更好的方法来做这件事,但我不知道如何继续。numpy可能会在这里提供帮助吗?熊猫?我尝试过用Cython编译代码,但没有多大帮助
我的函数接收一个列表,其中包含按数字顺序排列的马及其EV。(因为马的起始数字不是以零开始的,所以我将1添加到索引中)。我遍历所有不同的种族,并将组合的输出保存到数据帧中
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
为什么不试试这个,看看你是否可以毫无问题地运行这个函数?这可以在我的笔记本电脑上使用(我正在使用PyCharm)。如果你不能运行这个,那么我会说你可能需要一台更好的电脑。我没有遇到任何内存错误
假设我们有以下情况:
我重新编写了如下函数-在枚举中进行了更改。另外,不使用df,因为我不知道这是什么函数-
appendCombinationToDF
执行
len(l)
,我得到:如果可以执行上述操作,请将列表
l
替换为df
,并查看函数是否可以执行而不会遇到内存错误。我能够在不到20-30秒内完成上述操作相关问题 更多 >
编程相关推荐