I would like to unpack the variables from a list of list of tuples. I have made some attempts but could not reach a solution.
Here is a list of points. I would like to unpack each variable inside p1
, p2
, p3
, p4
during each iteration of the sublists so that during each iteration each variable gets assigned.
For example in the first iteration, I would expect:
p1=(0,0)
p2=(1,0)
p3=(2,0)
p4=(0,1)
In the next iteration:
p1=(0,0)
p2=(1,0)
p3=(2,0)
p4=(2,1)
points=[[(0, 0), (1, 0), (2, 0), (0, 1)],[(0, 0), (1, 0), (2, 0), (2, 1)]]
for i in points:
for j in i:
p1=j[0],j[1]
p2=j[0],j[1]
p3=j[0],j[1]
p4=j[0],j[1]
CodePudding user response:
Each element of points
is a list of tuples that can be unpacked, like so:
for p1, p2, p3, p4 in points:
print(p1, p2, p3, p4)
This outputs:
(0, 0) (1, 0) (2, 0) (0, 1)
(0, 0) (1, 0) (2, 0) (2, 1)
CodePudding user response:
I corrected sub to points and I fixed the index that was out of bounds when unpacking p2.
points=[[(0, 0), (1, 0), (2, 0), (0, 1)],[(0, 0), (1, 0), (2, 0), (2, 1)]]
for i in points:
for j in i:
p1=j[0],j[1]
p2=j[0],j[1]
p3=j[0],j[1]
p4=j[0],j[1]
print (p1,p2,p3,p4)