我正在使用具有以下结构/条目的数组(用于量子信息游戏中的硕士项目);第 1 列条目{0,1}
,第 2 列{0,1}
,第 3 列{0,2**(d-1)}
,最后列{0,d-1}
。如下d=3
:
G =
[[0 0 0 0]
[0 0 0 1]
[0 0 0 2]
[0 0 1 0]
[0 0 1 1]
[0 0 1 2]
[0 0 2 0]
[0 0 2 1]
[0 0 2 2]
[0 0 3 0]
[0 0 3 1]
[0 0 3 2]
[0 1 0 0]
[0 1 0 1]
[0 1 0 2]
[0 1 1 0]
[0 1 1 1]
[0 1 1 2]
[0 1 2 0]
[0 1 2 1]
[0 1 2 2]
[0 1 3 0]
[0 1 3 1]
[0 1 3 2]
[1 0 0 0]
[1 0 0 1]
[1 0 0 2]
[1 0 1 0]
[1 0 1 1]
[1 0 1 2]
[1 0 2 0]
[1 0 2 1]
[1 0 2 2]
[1 0 3 0]
[1 0 3 1]
[1 0 3 2]
[1 1 0 0]
[1 1 0 1]
[1 1 0 2]
[1 1 1 0]
[1 1 1 1]
[1 1 1 2]
[1 1 2 0]
[1 1 2 1]
[1 1 2 2]
[1 1 3 0]
[1 1 3 1]
[1 1 3 2]]
我正在使用以下函数来构建这个数组:
def games(d = 3):
res = np.empty(0).astype(int)
for a in range(2):
for b in range(2):
for x in range(2**(d-1)):
for y in range(d):
res = np.append(res,[a,b,x,y],axis=0)
res = np.reshape(res,(-1,4))
return res
现在我想做的是轻松选择列中条目开始计数的顺序。(从右列到左上方。)
例如,说我喜欢从第 1 列开始计数,然后是第 3 列,然后是第 4 列,最后是第 2 列。for-loops
我可以通过在函数中置换来得到这个:
def games(d = 3):
res = np.empty(0).astype(int)
for b in range(2):
for y in range(d):
for x in range(2**(d-1)):
for a in range(2):
res = np.append(res,[a,b,x,y],axis=0)
res = np.reshape(res,(-1,4))
return res
这使:
G=
[[0 0 0 0]
[1 0 0 0]
[0 0 1 0]
[1 0 1 0]
[0 0 2 0]
[1 0 2 0]
[0 0 3 0]
[1 0 3 0]
[0 0 0 1]
[1 0 0 1]
[0 0 1 1]
[1 0 1 1]
[0 0 2 1]
[1 0 2 1]
[0 0 3 1]
[1 0 3 1]
[0 0 0 2]
[1 0 0 2]
[0 0 1 2]
[1 0 1 2]
[0 0 2 2]
[1 0 2 2]
[0 0 3 2]
[1 0 3 2]
[0 1 0 0]
[1 1 0 0]
[0 1 1 0]
[1 1 1 0]
[0 1 2 0]
[1 1 2 0]
[0 1 3 0]
[1 1 3 0]
[0 1 0 1]
[1 1 0 1]
[0 1 1 1]
[1 1 1 1]
[0 1 2 1]
[1 1 2 1]
[0 1 3 1]
[1 1 3 1]
[0 1 0 2]
[1 1 0 2]
[0 1 1 2]
[1 1 1 2]
[0 1 2 2]
[1 1 2 2]
[0 1 3 2]
[1 1 3 2]]
在函数中排列 for 循环的顺序是可行的,但我必须编写 24 种不同的情况来涵盖所有排列。任何人都知道通常会更好solution/approach
吗?