我尝试做的示例:使用范围(2)转换d=2的功率集:
[(), (0,), (1,), (0, 1)]
或使用范围(1,3,1):
[(), (1,), (2,), (1, 2)]
转换为以下阵列阵列:
[[0, 0], [1, 0], [0, 1], [1, 1]]
更一般地说,我试图将indexSet(正数d的幂集)转换为包含标准基本向量的数组,其中元素(I,j,k)对应于d维空间中条目I、j和k中为1的向量(数组),空集对应于d空间中的零向量(条目中只有0的d元组数组)。
我做这件事很难。如有任何建议,我们将不胜感激。
from itertools import chain, combinations
d=5
indexSet = [[] for _ in range(1)]
NumbersUpToD = range(1,int(d)+1,1)
vect = [[0 for i in range(d)] for x in range(d)]
for i in range(d):
vect[i][i] = vect[i][i] + 1
set_of_d = set(NumbersUpToD)
numbers = [i for i in range(1)]
for z in chain.from_iterable(combinations(set_of_d,r) for r in range(len(set_of_d)+1)):
indexSet[numbers[0]].append(z)
indexSet = sum(indexSet, [])
for j in range(2**d):
str(list(indexSet[j]))
print(indexSet)
powerSetVectors = [[0 for x in range(d)] for i in range(2**d)]
print(powerSetVectors)
for i in range(2**d):
for j in range(d):
if j in indexSet[j]:
indexSet[j]=vect[j]
print(indexSet)
请注意,您应该看到的两组输出如下:
[(), (0,), (1,), (2,), (3,), (4,), (0, 1), (0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4), (0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 2, 3), (0, 2, 4), (0, 3, 4), (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4), (0, 1, 2, 3), (0, 1, 2, 4), (0, 1, 3, 4), (0, 2, 3, 4), (1, 2, 3, 4), (0, 1, 2, 3, 4)]
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
我的好朋友帮我回答了这个问题。
这里有一个替代解决方案:
def pwr(d,r):
out = []
for n in range(2**d):
p = [r*int(i) for i in list(bin(n)[2:])]
while len(p) < d:
p = [0] + p
out.append(p)
return out
print(out)
print(pwr(2,1))
输出为:
[[0, 0], [0, 1], [1, 0], [1, 1]]