Oleg Melnikov
Oleg Melnikov

Reputation: 3298

Re-group 2D coordinates of pairs in Python

How can I rearrange [(0, 4), (1, 3)] to (0, 1), (4, 3), in other words group first coordinates together and second coordinates together (order maintained left to right). I need to do this for a large list of, exampled below

c = [[(0, 4), (1, 3)],
     [(0, 4), (1, 5)],
     [(1, 3), (2, 2)],
     [(1, 3), (2, 4)],
     [(1, 5), (2, 2)],
     [(1, 5), (2, 6)],
     ...]

So that the end result is

[[(0, 1), (4, 3)],
 [(0, 1), (4, 5)],
 [(1, 2), (3, 2)],
 [(1, 2), (3, 4)],
 [(1, 2), (5, 2)],
 [(1, 2), (5, 6)],
 ...
 ]

Upvotes: 1

Views: 72

Answers (1)

David Eisenstat
David Eisenstat

Reputation: 65447

How about

end_result = [[(w, y), (x, z)] for [(w, x), (y, z)] in c]

?

Upvotes: 3

Related Questions