简体   繁体   English

Python:基于枚举列表的枚举

[英]Python: Enumeration based on an enumerated list

I have several vertexes that compose two triangles.我有几个顶点组成两个三角形。

Vertexes:顶点:

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.]]

Triangles:三角形:

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

Now I need to export this data to a.txt file, to have the following output composed by two different parts:现在我需要将此数据导出到 a.txt 文件,以具有由两个不同部分组成的以下 output:

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

The first part was easy, as I only needed to enumerate each vertex based on the aforementioned list.第一部分很简单,因为我只需要根据上述列表枚举每个顶点。

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()

The problem is with the second part, as I need to enumerate each triangle (first column) and then assign the values previously given in part 1 to each vertex that compose the triangle.问题出在第二部分,因为我需要枚举每个三角形(第一列),然后将先前在第 1 部分中给出的值分配给构成三角形的每个顶点。

eg triangle ABC is composed by vertexes 1, 2 and 3;例如三角形ABC由顶点1、2和3组成; triangle ACD is composed by vertexes 1,3 and 4, so the output (previously presented) should be:三角形 ACD 由顶点 1,3 和 4 组成,因此 output(之前给出的)应该是:

1 1 2 3
2 1 3 4

I would appreciate any help you could give me on this.如果您能在这方面给我任何帮助,我将不胜感激。 Thanks so much.非常感谢。

It looks like you would do something very similar.看起来你会做一些非常相似的事情。

This would get the vertices:这将获得顶点:

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)

This is the output:这是 output:

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

or more generally make a list of triagles and solve in the same way like this:或更一般地列出三角形并以相同的方式解决,如下所示:

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)

which gives this:这给了这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM