我有几行代码可以在facet列表中对元素进行排序。小平面是顶点列表,并且代码对它们进行排序,使得相邻元素形成边缘,即如果一对顶点包含在两个小平面中,则必须相邻。方向无关紧要。问题是第11行中的for循环(参见注释)忘记了第一个for循环已经完成的cx元素。
cx = [[0, 1, 2, 3], [4, 5, 6, 7, 8], [0, 1, 9, 10, 11], [12, 13, 14, 15, 16], [12, 13, 17, 18, 19], [20, 21, 22, 23], [9, 10, 24, 25], [17, 18, 26, 27], [20, 21, 28, 29], [20, 22, 28, 30, 31], [2, 3, 31, 30, 5, 4], [0, 2, 29, 30, 24, 28, 9], [6, 7, 14, 15], [1, 3, 14, 16, 6, 11, 4], [7, 8, 26, 17, 15, 12], [10, 11, 25, 19, 16, 13], [5, 8, 27, 31, 23, 26, 22], [18, 19, 25, 21, 24, 23, 27, 29]]
ordered = []
for i in range(0,len(cx)):
ordfacet = []
facet = cx[i]
ordfacet.append(facet[0])
del facet[0]
while len(facet) > 0:
for z in facet:
for y in cx: #this loop does not iterate over cx[k] for k=0,...,i-1
if all(x in y for x in [ordfacet[-1],z]):
ordfacet.append(z)
facet.remove(z)
break
ordered.append(ordfacet)
print(ordered)
奇怪的是,当我将cx的定义放在第一个for循环中时,它按预期工作然后我需要知道cx的长度来定义第一个循环的范围,这在实践中很烦人。有没有办法做到这一点,我需要做的就是在一个地方输入一个方面列表?感谢。