It is necessary to sort the list of coordinates (x, y, z) by two parameters:
- The entire list is sorted by ascending
Zcoordinate. - Tuples with the same
Zcoordinates are in turn sorted in descending order byYcoordinateY
At the entrance:
[(3, 11, 14), (3, 9, 18), (3, 10, 10), (3, 11, 12), (3, 11, 10), (3, 10, 12), (3, 9, 14)] At the exit:
[(3, 11, 10), (3, 10, 10), (3, 11, 12), (3, 10, 12), (3, 11, 14), (3, 9, 14), (3, 9, 18)] Code:
from itertools import groupby result = [] coord = [(3, 11, 14), (3, 9, 18), (3, 10, 10), (3, 11, 12), (3, 11, 10), (3, 10, 12), (3, 9, 14)] for key, groups in groupby(sorted(coord, key=lambda z: z[2]), lambda z: z[2]): for group in sorted(groups, key=lambda y: y[1], reverse=True): result.append(group) print(result) # [(3, 11, 10), (3, 10, 10), (3, 11, 12), (3, 10, 12), (3, 11, 14), (3, 9, 14), (3, 9, 18)] What other solutions could there be?