Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Iterating over two list of lists, getting the same element of each inner list

I am working with ETABS API using python. So there is a command like:

SapModel.FrameObj.AddByCoord(x1,y1,z1,x2,y2,z2,"name","section")

where x1 , x2 ,etc are coordinates in Cartesian coordinate system.
I have two separate lists that each one consist of 5 sub lists and each sub list contain 5 elements that are X and Y coordinates of different points. Some thing like:

X=[[2,4,5,9,10],[3,6,7,9,11],[10,16,17,19,20],[8,9,15,16,20],[3,12,14,18,21]]
Y=[[0,5,6,8,10],[1,3,7,8,16],[10,13,14,18,23],[6,8,11,14,16],[5,14,15,19,20]]

The output I’m looking for is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for i in range(3,16,3):
    SapModel.FrameObj.AddByCoord(X[0][0],Y[0][0],i,X[1][0],Y[1][0],i,"name","section")
    SapModel.FrameObj.AddByCoord(X[1][0],Y[1][0],i,X[2][0],Y[2][0],i,"name","section")
    SapModel.FrameObj.AddByCoord(X[2][0],Y[2][0],i,X[3][0],Y[3][0],i,"name","section")
    SapModel.FrameObj.AddByCoord(X[3][0],Y[3][0],i,X[4][0],Y[4][0],i,"name","section")
    
    SapModel.FrameObj.AddByCoord(X[0][1],Y[0][1],i,X[1][1],Y[1][1],i,"name","section")
    SapModel.FrameObj.AddByCoord(X[1][1],Y[1][1],i,X[2][1],Y[2][1],i,"name","section")
    SapModel.FrameObj.AddByCoord(X[2][1],Y[2][1],i,X[3][1],Y[3][1],i,"name","section")
    ...
    ...
    ...
    SapModel.FrameObj.AddByCoord(X[0][4],Y[0][4],i,X[1][4],Y[1][4],i,"name","section")
    SapModel.FrameObj.AddByCoord(X[1][4],Y[1][4],i,X[2][4],Y[2][4],i,"name","section")
    SapModel.FrameObj.AddByCoord(X[2][4],Y[2][4],i,X[3][4],Y[3][4],i,"name","section")
    SapModel.FrameObj.AddByCoord(X[3][4],Y[3][4],i,X[4][4],Y[4][4],i,"name","section")

I have no idea how to do that. Can you help me with that?

>Solution :

You can iterate as follows

for _x, _xs, _y, _ys in zip(X,Y,X[1:],Y[1:]):
    for x, y, xs, ys in zip(_x, _xs, _y, _ys):
        print(x, y, xs, ys)

or

for _x, _xs, _y, _ys in zip(zip(*X), zip(*Y) , zip(*X[1:]), zip(*Y[1:])):
    for x, y, xs, ys in zip(_x, _xs, _y, _ys):
        print(x, y, xs, ys)

The second way would produce 4-pairs in the exact order as in your example:

2 0 3 1
3 1 10 10
10 10 8 6
8 6 3 5
4 5 6 3
6 3 16 13
16 13 9 8
9 8 12 14
5 6 7 7
7 7 17 14
17 14 15 11
15 11 14 15
9 8 9 8
9 8 19 18
19 18 16 14
16 14 18 19
10 10 11 16
11 16 20 23
20 23 20 16
20 16 21 20
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading