It is necessary to sort the list of coordinates (x, y, z) by two parameters:

  1. The entire list is sorted by ascending Z coordinate.
  2. Tuples with the same Z coordinates are in turn sorted in descending order by Y coordinate Y

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?

    1 answer 1

    You can try this:

     coord = [(3, 11, 14), (3, 9, 18), (3, 10, 10), (3, 11, 12), (3, 11, 10), (3, 10, 12), (3, 9, 14)] print(sorted(coord, key=lambda point: (point[2], -point[1])))