Обобщенный подход будет выглядеть примерно так:
alist = [(1,6),(2,5),(2,4),(7,5)]
temp = map(sorted, zip(*alist))
min_x, max_x, min_y, max_y = temp[0][0], temp[0][-1], temp[1][0], temp[1][-1]
Для Python 3 вам нужно изменить строку, которая создает temp
, на:
temp = tuple(map(sorted, zip(*alist)))
Идеяможно абстрагировать в функцию, которая работает как в Python 2, так и в 3:
from __future__ import print_function
try:
from functools import reduce # moved into functools in release 2.6
except ImportError:
pass
# readable version
def minmaxes(seq):
pairs = tuple()
for s in map(sorted, zip(*seq)):
pairs += (s[0], s[-1])
return pairs
# functional version
def minmaxes(seq):
return reduce(tuple.__add__, ((s[0], s[-1]) for s in map(sorted, zip(*seq))))
alist = [(1,6), (2,5), (2,4), (7,5)]
min_x, max_x, min_y, max_y = minmaxes(alist)
print(' '.join(['{},{}']*2).format(*minmaxes(alist))) # 1,7 4,6
triplets = [(1,6,6), (2,5,3), (2,4,9), (7,5,6)]
min_x, max_x, min_y, max_y, min_z, max_z = minmaxes(triplets)
print(' '.join(['{},{}']*3).format(*minmaxes(triplets))) # 1,7 4,6 3,9