-
python3 customized compare function아무거나적어~ 2022. 9. 24. 16:42
https://stackoverflow.com/questions/2531952/how-to-use-a-custom-comparison-function-in-python-3
How to use a custom comparison function in Python 3?
In Python 2.x, I could pass custom function to sorted and .sort functions >>> x=['kar','htar','har','ar'] >>> >>> sorted(x) ['ar', 'har', 'htar', 'kar'] >>> &g...
stackoverflow.com
https://stackoverflow.com/questions/5213033/sort-a-list-of-lists-with-a-custom-compare-function
Sort a list of lists with a custom compare function
I know there are several questions named like this, but they don't seem to work for me. I have a list of lists, 50 times 5 elements. I want to sort this list by applying a custom compare function to
stackoverflow.com
12345678910111213import sysfrom functools import cmp_to_keydef compare(a, b): # a가 왼쪽 원소, b가 오른쪽 원소if b[1] != a[1]: return b[1] - a[1] # 이게 양수면 a, b 바꿈else:if b[0] == a[0]: return 0 # 0 이면 a, b간 선후관계 없음else: return b[0] - a[0]n = int(input())v = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]v.sort(key=cmp_to_key(compare))print(v)cs'아무거나적어~' 카테고리의 다른 글
definition of lower_bound and upper_bound (0) 2022.09.27 선분교차 (0) 2022.09.26 동아리 등록하신 분 백준 그룹에 등록하기 (0) 2022.09.03 백준 문제집 만들기 부분-자동화 (0) 2022.08.22 Markdown with LaTex (0) 2022.07.15