所以我开始为 4D 立方体 (tesseract) 制作这个,但后来有点困惑,所以我把它降低了一个维度。
我试图找出一种方法来迭代遍历我命名为“pos”的列表(基本上是一个 1 和 0 的列表,指示节点的位置)并填写相关值。
例如 原点为 [0,0,0],而 'pqr' 为 [1,1,1]。
我想对一般数量的维度执行此操作,我认为我找到了正确的启动方式。它首先按长度对节点进行排序,然后按字典顺序对节点进行排序。之后,它检查节点的长度,如果它在正确的维度中,那么它可以遍历“pos”列表,将 1 添加到适当的字段。它向前看,如果下一个节点是更高的维度,那么它会改变它添加到下一个维度的方式。这是我需要帮助的地方。
我如何遍历此列表以在适当的维度上加 1 来为我的立方体制作脚手架?
输入
#!/usr/bin/python
axis_labels = ['p','q','r']
dimension = len(axis_labels)
D_node_edge = {'pr': ['pqr'], 'pq': ['pqr'], 'p': ['pq', 'pr'], 'qr': ['pqr'], 'q': ['pq', 'qr'], '0': ['p', 'q', 'r'], 'pqr': [], 'r': ['pr', 'qr']}
D_node_pos = {}
pos = [0 for p in xrange(dimension)]
ordered_nodes = sorted(D_node_edge, key = lambda x: (len(x),x))
dim_tmp = 1
D_node_pos['0'] = pos
for i in xrange(1,2**dimension-1):
node = ordered_nodes[i]
n_node = ordered_nodes[i+1]
if len(node) == dim_tmp:
#need help right here to go through and add relevant entries to 'pos'
D_node_pos[node] = pos
if len(n_node) > dim_tmp:
dim_tmp += 1
D_node_pos.items() 的期望输出值
0 [0,0,0]
p [1,0,0]
q [0,1,0]
r [0,0,1]
pq [1,1,0]
pr [1,0,1]
qr [0,1,1]
pqr [1,1,1]
请您参考如下方法:
如果你只想要顶点列表,你可以很容易地做到:
>>> map(list, product([0, 1], repeat=dimension))
或
>>> sorted(map(list, product([0, 1], repeat=dimension)), key=sum)
[[0, 0, 0],
[0, 0, 1],
[0, 1, 0],
[1, 0, 0],
[0, 1, 1],
[1, 0, 1],
[1, 1, 0],
[1, 1, 1]]
否则,如果您还想要标签组合,您可以尝试类似的方法:
from itertools import product
axis_labels = ['p','q','r']
dimension = len(axis_labels)
for comb in product([0, 1], repeat=dimension):
label = ''.join([axis_labels[i] for i in range(dimension) if comb[i]])
if not label: label = '0'
print label, list(comb)
产量
0 [0, 0, 0]
r [0, 0, 1]
q [0, 1, 0]
qr [0, 1, 1]
p [1, 0, 0]
pr [1, 0, 1]
pq [1, 1, 0]
pqr [1, 1, 1]
如果您关心顺序,您可以随时存储标签/梳子并按标签长度排序。
