Python Itertools.product 加速

Python Itertools.product speed up

AI 有多个数组,我要放入 itertools.product()

数组长度:

A - 108
B - 126
C - 5
D - 8
E  -8

我花了好几分钟来处理数组,我想知道是否有其他方法可以加快速度。

total = 0
maxPoints = 0

for combination in itertools.product(A, B, C, D, E):
currentTotal  = int(combination[0][1]) + int(combination[1][0][1]) + int(combination[1][1][1]) + int(combination[1][2][1]) +int(combination[2][0][1]) + int(combination[2][1][1])  + int(combination[2][2][1])  + int(combination[1][0][1])  + int(combination[1][1][1])
if currentTotal < 50000:
    pointTotal  = float(combination[0][2]) + float(combination[1][0][2]) + float(combination[1][1][2]) + float(combination[1][2][2]) + float(combination[2][0][2]) + float(combination[2][1][2])  + float(combination[2][2][2]) +  float(combination[1][0][2]) + float(combination[1][1][2])
    if currentTotal >= total:
        if pointTotal > maxPoints:
            total = currentTotal
            maxPoints = currentPoints

您可能无法仅使用纯 python 代码进一步优化它。

对于任何严重的数字运算,我建议使用 numpy。

Here 是 numpy 中的卡特尔乘积实现,您可以使用: