使用Items out列表作为变量名



我在编写一些python代码时遇到了问题。我将陈述这个案子,希望你能帮助我。我从外部软件获得一个包含有关结构信息的xml文件。就像这个xml文件中不同的点和线一样。下面这里是这个XML文件的一小部分

<container id="{XXXXXXX}" t="EP_DSG_Elements.EP_StructNode.1">
<table id="XXXXXXX" t="EP_DSG_Elements.EP_StructNode.1" name="Node">
<h>
<h0 t="Name"/>
<h1 t="Coord X"/>
<h2 t="Coord Y"/>
<h3 t="Coord Z"/></h>
<obj id="1" nm="K1">
<p0 v="K1"/>
<p1 v="0"/>
<p2 v="0"/>
<p3 v="0"/></obj>

<obj id="2" nm="K2">
<p0 v="K2"/>
<p1 v="0"/>
<p2 v="0"/>
<p3 v="3.6000000000000001"/></obj>

<obj id="3" nm="K3">
<p0 v="K3"/>
<p1 v="0"/>
<p2 v="0"/>
<p3 v="7.2000000000000002"/></obj>
<container id="{XXX}" t="EP_DSG_Elements.EP_Beam.1">
<table id="XXXX" t="EP_DSG_Elements.EP_Beam.1" name="Member 1D">
<h>
<h0 t="Name"/>
<h1 t="Type"/>
<h2 t="Beg. node"/>
<h3 t="End node"/>
<h4 t="Cross-section"/>
<h5 t="FEM type"/>
<h6 t="Member system-line at"/>
<h7 t="ey"/>
<h8 t="ez"/>
<h9 t="Table of geometry"/></h>
<obj id="1" nm="S1">
<p0 v="S1"/>
<p1 v="2" t="column (100)"/>
<p2 i="1" n="K1"/>
<p3 i="2" n="K2"/>
<p4 i="3" n="CS3 - IPE300"/>
<p5 v="0" t="standard"/>
<p6 v="1" t="Centre"/>
<p7 v="0"/>
<p8 v="0"/>
<p9 t="">
<h>
<h1 t="Node"/>
<h2 t="Edge"/>
</h>
<row id="0">
<p1 i="1" n="K1"/>
<p2 v="0" t="Lijn"/>
</row>
<row id="1">
<p1 i="2" n="K2"/>
</row>
</p9></obj>

xml文件是自动生成的,点数是可变的。结构是这样的:结构

我能分辨出哪些是水平的,哪些是垂直的,因为我只对垂直的感兴趣。

问题就在这里。因为行和点的数量是可变的,我想使用一个while循环,它具有子元素长度的条件(len(myroot[8][0]))。因此,我想为每条垂直线创建一个列表。这些列表必须包括位于该线上方的所有点。第9行的例子:[8,6,5]和第14行:[10,11]有简单的方法吗?

这是我现在得到的:首先我做了3个列表,分别包含所有的X, Y和Z坐标。我使用了以下代码:

List_points = []
List_All_Points_X = []
List_All_Points_Y = []
List_All_Points_Z = []
i=1
while i < len(myroot[7][0]):
VarPointID = myroot[7][0][i].attrib
PointID = str(VarPointID.get('id'))
List_Points.append(int(KnoopID))
VarXCoord = myroot[7][0][i][1].attrib
XCoord = str(VarXCoord.get('v'))
List_All_Points_X.append(int(XCoord))
VarYCoord = myroot[7][0][i][2].attrib
YCoord = str(VarYCoord.get('v'))
List_All_Points_Y.append(int(YCoord))
VarZCoord = myroot[7][0][i][3].attrib
ZCoord = str(VarZCoord.get('v'))
List_All_Points_Z.append(float(ZCoord))
i = i+1

我过滤了所有的重复,并将它们从低到高排序,所以我有一个想法,使用的坐标是在结构

for i in List_All_Points_X:
if i not in List_Points_X:
List_Points_X.append(i)
for i in List_All_Points_Y:
if i not in List_Points_Y:
List_Points_Y.append(i)
for i in List_All_Points_Z:
if i not in List_Points_Z:
List_Points_Z.append(i)
List_Points_X.sort(reverse=False)
List_Points_Y.sort(reverse=False)
List_Points_Z.sort(reverse=False)

但是现在我被困在为每一行创建多个迭代列表,有人能帮我吗,或者有更好的方法来做这一切?

编辑该文件的输出将生成新的xml文件,这些文件将加载到程序中,看起来像这样:新结构为了创建新的结构,我必须删除一条线,并将线上方的点向下移动。当点的坐标改变时,水平线将自动移动。但是因为一开始线和点的数量是未知的我需要让它迭代。我创建了一个列表,其中包含所有垂直的行号,这样我就知道每次要删除哪些行。但问题在于如何移动这些点。我想为每条线制作一个列表,包括该线上方的所有水平点。为了创建多个列表而不需要迭代,只需要手动操作我希望它是这样的:

Line1=[]
Line2=[]
Line3=[]
Line4=[]
Line5=[]
Line6=[]
Line7=[]
Line8=[]
Line9=[]
Line10=[]

I将检查所有点的所有x坐标,并列出在同一垂直空间中的所有点。我将排序这个列表,使列表的第一个成员是最低点,列表的最后一个成员是最高点。

ListFirstRow=[1,2,3,4]
ListSecondRow=[5,6,7,8]
ListThirdRow=[9,10,11,12]
SortedListFirstRow=[1,2,3,4]
SortedListSecondRow=[7,8,6,5]
SortedListThirdRow=[12,9,10,11]

当我删除该行时,我可以在XML文件中找到该行的开始和点。我将在包含垂直空间点的列表中查找这两个点并检查它们的索引。然后我就可以遍历这个列表,起始索引是行结束点的索引,并进行这些更改。

假设我想创建一个删除第一行的结构。然后在XML文件中检查第1行的起始点和结束点。

<container id="{XXX}" t="EP_DSG_Elements.EP_Beam.1">
<table id="XXXX" t="EP_DSG_Elements.EP_Beam.1" name="Member 1D">
<h>
<h0 t="Name"/>
<h1 t="Type"/>
<h2 t="Beg. node"/>
<h3 t="End node"/>
<obj id="1" nm="S1">
<p0 v="S1"/>
<p1 v="2" t="column (100)"/>
<p2 i="1" n="K1"/>
<p3 i="2" n="K2"/>

起点K1和终点K2。我现在可以在列表SortedListFirstRow=[1,2,3,4]中检查哪个点是最高的。在这种情况下,点2或K2是最高的,我找SortedListFirstRow.index(2)的指数,它等于1。我可以像这样创建一个while循环:

i = SortedListFirstRow.index(2)
while i < len(SortedListFirstRow)
VarZCoord = myroot[7][0][i][3].attrib
ZCoord = str(VarZCoord.get('v'))
New_Value = float(ZCoord)-1
myroot[7][0][i][3].clear()
myroot[7][0][i][3].set('v', New_Value)

像这样,我可以改变点的坐标在这个例子中需要向下移动一点

我希望我已经说清楚了,已经谢谢你们所有人回答我的问题!

提前感谢,尼尔斯·

我成功地使用了字典。谢谢你的帮助!

最新更新