I'm developing a code in ETABS API using python. There is a command for drawing beams. it is like:
SapModel.FrameObj.AddByCoord(X1,Y1,Z1,X2,Y2,Z2,"name","section")
where X1,Y1,Z1,etc are coordinates in Cartesian coordinate system. I have two list of lists that each of them are X and Y coordinates. Some thing like:
XCoordsList=[[0,5.5,11],[0,6,12.3],[0,7,12.5]]
YCoordsList=[[5,6.1,5.7],[10.5,12.1,12.7],[16,18,19]]
These lists are Corresponding, that means X and Y of a certain point is (XCoordList[0,0],YCoords[0,0]);(e.g.:(0,5),(5.5,6.1),(11,5.7)) I want to iterate on these two list and draw my beams.So this is what I have written:
Z=3
for m,n in zip(XCoordsList,YCoordsList):
SapModel.FrameObj.AddByCoord(m[m],n[n],Z,m[m 1],n[n 1],Z,"name","section")
and the result is: TypeError: list indices must be integers or slices, not list. What should I do?
CodePudding user response:
You problem is the bad indexing. I have implement a working solution with some comment for better understanding.
My solution is also handling the X 1
indexing in your formula. You can see in the code my snippet prints the all required parameters.
Code:
XCoordsList = [[0, 5.5, 11], [0, 6, 12.3], [0, 7, 12.5]]
YCoordsList = [[5, 6.1, 5.7], [10.5, 12.1, 12.7], [16, 18, 19]]
Z = 3
for m, n in zip(XCoordsList, YCoordsList):
for cont, (x, y) in enumerate(zip(m, n)): # Iterate on the nested (inside) list pairs
if cont 1 in [len(m), len(n)]: # Test if there is no more item for "cont 1"
continue # If there is not more item, get the nest nested list pair
print(x, y, Z, m[cont 1], n[cont 1], Z, "name", "section") # Print out the result
Output:
>>> python3 test.py
0 5 3 5.5 6.1 3 name section
5.5 6.1 3 11 5.7 3 name section
0 10.5 3 6 12.1 3 name section
6 12.1 3 12.3 12.7 3 name section
0 16 3 7 18 3 name section
7 18 3 12.5 19 3 name section
CodePudding user response:
How about this approach?
XCoordsList=[[0,5.5,11],[0,6,12.3],[0,7,12.5]]
YCoordsList=[[5,6.1,5.7],[10.5,12.1,12.7],[16,18,19]]
Coords = [
(XCoordsList[array_idx][idx],YCoordsList[array_idx][idx])
for array_idx in range(len(XCoordsList))
for idx in range(len(XCoordsList[array_idx]))
]
print(Coords)
You get:
[(0, 5), (5.5, 6.1), (11, 5.7), (0, 10.5), (6, 12.1), (12.3, 12.7), (0, 16), (7, 18), (12.5, 19)]
CodePudding user response:
It's probably better to pair them up in a new structure, then use that structure.
z = 3
assert len(XCoordsList) == len(YCoordsList)
obj_list = []
for i in range(len(XCoordsList)):
assert len(XCoordsList[i]) == len(YCoordsList[i])
obj_list.extend((XCoordsList[i][j], YCoordsList[i][j], z, XCoordsList[i][j 1], YCoordsList[i][j 1], z) for j in range(len(0, XCoordsList[i], 2)))
Now, you have a representation you can easily loop over and otherwise manipulate (delete individual points, slice it into smaller parts, etc).
for x1, y1, z1, x2, y2, z2 in obj_list:
SapModel.FrameObj.AddByCoord(
x1, y1, z1, x2, y2, z2, "name", "section")
It's not clear from your question why you have nested lists; if you want to keep that structure, it should be easy to see how to change the code to preserve that.
CodePudding user response:
Even if other answers go more in depth with more elegant code, I suspect that you're new to Python, because of this question about indexes, so I made my example the simplest as possible.
You want your items in the sublists to be indexed from 0 to 2 (being their length 3):
XCoordsList=[[0,5.5,11], [0,6,12.3], [0,7,12.5]]
YCoordsList=[[5,6.1,5.7], [10.5,12.1,12.7], [16,18,19]]
for m,n in zip(XCoordsList, YCoordsList):
print(f"m: {m}\nn: {n}")
for i in range(3):
print(f"m[{i}], n[{i}]: {m[i]}, {n[i]}")
print("----------------------------")
Which prints:
m: [0, 5.5, 11]
n: [5, 6.1, 5.7]
m[0], n[0]: 0, 5
m[1], n[1]: 5.5, 6.1
m[2], n[2]: 11, 5.7
----------------------------
m: [0, 6, 12.3]
n: [10.5, 12.1, 12.7]
m[0], n[0]: 0, 10.5
m[1], n[1]: 6, 12.1
m[2], n[2]: 12.3, 12.7
----------------------------
m: [0, 7, 12.5]
n: [16, 18, 19]
m[0], n[0]: 0, 16
m[1], n[1]: 7, 18
m[2], n[2]: 12.5, 19
----------------------------
As you can see m[m],n[n],m[m 1],n[n 1]
, is not the way to go: you're indexing by the lists themselves, and the interpreter is telling you it's illegal.