Reputation: 1756
I've an array of this format:
val = [[1302, 303, 168, 536],
[1424, 360, 226, 677],
[776, 321, 194, 509],
[1066, 276, 191, 571]]
I'm extracting first and second element of the array in the following way:
x = [col for col in list(zip(*val))[0]]
y = [col for col in list(zip(*val))[1]]
Creating an empty set:
points = set()
Creating a tuple with combination of list:
center = tuple(zip(x,y))
Appending the list in the point dataset:
points.add(center)
Finally trying to print one list at a time:
for point in points:
print(point)
I'm getting the following result:
((1302, 303), (1424, 360), (776, 321), (1066, 276))
But I want it should print:
First, (1302, 303)
Second, (1424, 360)
and so on.
Can someone please guide me how to do it?
Thanks!
Upvotes: 0
Views: 276
Reputation: 26047
You can just iterate over your original list:
for x in val:
print(tuple(x[:2]))
Or from your points
using unpacking and passing sep
:
points = ((1302, 303), (1424, 360), (776, 321), (1066, 276))
print(*points, sep='\n')
# (1302, 303)
# (1424, 360)
# (776, 321)
# (1066, 276)
Upvotes: 1
Reputation: 2280
I find this too complicated:
x = [col for col in list(zip(*val))[0]]
y = [col for col in list(zip(*val))[1]]
Instead, you could do:
x = [col[0] for col in val]
y = [col[1] for col in val]
Then, create your points data structure:
points = set([(i, j) for i, j in zip(x, y)])
Print your points:
for point in points:
print(point)
Upvotes: 1
Reputation: 20500
Just use a double for-loop
#Iterate over the set
for point in points:
#Iterate on each point
for item in point:
print(item)
This will output
(1302, 303)
(1424, 360)
(776, 321)
(1066, 276)
To print it in a single loop, you need to modify your points
set to a 1-D list
val = [[1302, 303, 168, 536],
[1424, 360, 226, 677],
[776, 321, 194, 509],
[1066, 276, 191, 571]]
x = [col for col in list(zip(*val))[0]]
y = [col for col in list(zip(*val))[1]]
points = set()
center = tuple(zip(x,y))
points.add(center)
#Convert set to a 1-D list
points = list(*points)
for point in points:
print(point)
This will output
(1302, 303)
(1424, 360)
(776, 321)
(1066, 276)
Or a one-liner print
without loops
val = [[1302, 303, 168, 536],
[1424, 360, 226, 677],
[776, 321, 194, 509],
[1066, 276, 191, 571]]
x = [col for col in list(zip(*val))[0]]
y = [col for col in list(zip(*val))[1]]
points = set()
center = tuple(zip(x,y))
points.add(center)
#One liner print
print(*list(*points), sep='\n')
Upvotes: 1