Python: 基于枚举列表的枚举

Python: Enumeration based on an enumerated list

我有几个顶点组成两个三角形。

顶点:

A = [0.,0.,0.]
B = [2.,4.,0.]
C = [2.,3.,0.]
D = [1.,1.,0.]
vertex_list=[[0.,0.,0.],[2.,4.,0.],[2.,3.,0.],[1.,1.,0.]]

三角形:

ABC=[[0.,0.,0.],[2.,4.,0.],[2.,3.,0.]]
ACD=[[0.,0.,0.],[2.,3.,0.],[1.,1.,0.]]

现在我需要将此数据导出到一个 .txt 文件,以得到由两个不同部分组成的以下输出:

1 0.0 0.0 0.0
2 2.0 4.0 0.0
3 2.0 3.0 0.0
4 1.0 1.0 0.0
end part 1
1 1 2 3
2 1 3 4
end part 2

第一部分很简单,因为我只需要根据上述列表枚举每个顶点。

file=open("test.dat","w")    
for i,list in enumerate(vertex_list,start=1):
        file.write("{} {} {} {}\n".format(i,list[0],list[1],list[2]))
file.close()

问题出在第二部分,因为我需要枚举每个三角形(第一列),然后将先前在第 1 部分中给出的值分配给构成三角形的每个顶点。

例如三角形ABC由顶点1、2、3组成;三角形 ACD 由顶点 1,3 和 4 组成,因此输出(之前给出的)应该是:

1 1 2 3
2 1 3 4

如果您能在这方面给我任何帮助,我将不胜感激。 非常感谢。

看起来你会做一些非常相似的事情。

这将得到顶点:

vertex_list=[[0.,0.,0.],[2.,4.,0.],[2.,3.,0.],[1.,1.,0.]]

ABC=[[0.,0.,0.],[2.,4.,0.],[2.,3.,0.]]
ACD=[[0.,0.,0.],[2.,3.,0.],[1.,1.,0.]]

print('solve ABC')
for i in ABC:
    for j,k in enumerate(vertex_list, start=1):
        if i==k:
            print('vertex found at', j)

print('solve ACD')
for i in ACD:
    for j,k in enumerate(vertex_list, start=1):
        if i==k:
            print('vertex found at', j)

这是输出:

solve ABC
vertex found at 1
vertex found at 2
vertex found at 3
solve ACD
vertex found at 1
vertex found at 3
vertex found at 4

或更一般地列出三角形并以同样的方式求解:

vertex_list=[[0.,0.,0.],[2.,4.,0.],[2.,3.,0.],[1.,1.,0.]]

ABC=[[0.,0.,0.],[2.,4.,0.],[2.,3.,0.]]
ACD=[[0.,0.,0.],[2.,3.,0.],[1.,1.,0.]]

triamgles_list = [ABC, ACD]

for c, t in enumerate(triamgles_list):
    print('solve item', c)
    for i in t:
        for j,k in enumerate(vertex_list, start=1):
            if i==k:
                print('vertex found at', j)

这给出了这个:

solve item 0
vertex found at 1
vertex found at 2
vertex found at 3
solve item 1
vertex found at 1
vertex found at 3
vertex found at 4