我想考虑自然数的几种组合。现在我花了很多时间甚至使用N = 3,我想介绍代码的并行化。这段代码在这里。对不起,大字符串!
from itertools import product
n1=[]
n2=[]
n3=[]
n4=[]
N=2
for i in range(N+1):
n1.append(i)
n2.append(i)
n3.append(i)
n4.append(i)
c=list(product(n1,n2,n3,n4))
for i in range(len(c)):
d=list(itertools.islice(c[i],4))
d=np.array(d)
n1=d[0]
n2=d[1]
n3=d[2]
n4=d[3]
modes=[]
ymain=[]
for i in range(n1+n2):
modes.append(1400)
for i in range(n3+n4):
modes.append(600)
for i in range(n1):
ymain.append(X)
for i in range(n2):
ymain.append(Y)
for i in range(n3):
ymain.append(X)
for i in range(n4):
ymain.append(Y)
for p in range(1,30):
E=500*p
numer=int(round(E/600.0))+1
M=np.size((np.array(modes)))
A=[]
for i in range(M):
A.append(range(numer))
for i in range (M):
for j in range(numer):
A[i][j]=(A[i][j])*modes[i]
modes=np.array(modes)
ymain=np.array(ymain)
sums = ((vs,sum(vs)) for vs in product(*A))
for k,v in sums:
print k,v, E
如何为1-12个内核并行化itertools.product?