[(0, 4), (1, 2), (1, -1), (2, 2), (3, 3)]
위와 같은 tuple을 포함한 리스트를
1) y좌표 오름차순
2) y좌표 같다면 x좌표 오름차순
으로 정리해 보자.
아래와 같은 결과가 예상된다.
[(1, -1), (1, 2), (2, 2), (3, 3), (0, 4)]
l1 = [(0, 4), (1, 2), (1, -1), (2, 2), (3, 3)]
l2 = sorted(l1, key =lambda x : (x[1],x[0]))
print(l2)
>>> [(1, -1), (1, 2), (2, 2), (3, 3), (0, 4)]
import functools
def comparator(a,b):
t1 = a+b
t2 = b+a
return (int(t1) > int(t2)) - (int(t1) < int(t2)) # t1이 크다면 1 // t2가 크다면 -1 // 같으면 0
def solution(numbers):
n = [str(x) for x in numbers]
n = sorted(n, key=functools.cmp_to_key(comparator),reverse=True)
answer = str(int(''.join(n)))
return answer
>>> [(1, -1), (1, 2), (2, 2), (3, 3), (0, 4)]