#튜플 정렬
#첫 번째 원소로 오름차순 정렬하기
#[(1, 2), (1, -1), (2, 2), (3, 4), (3, 3)]
tuple = [(3, 4), (2, 2), (3, 3), (1, 2), (1, -1)]
sort_tuple = sorted(tuple, key=lambda x : x[0])
print(sort_tuple)
#첫 번째 원소로 내림차순 정렬하기
#[(3, 4), (3, 3), (2, 2), (1, 2), (1, -1)]
sort_tuple = sorted(tuple, key=lambda x:-x[0])
print(sort_tuple)
#첫 번째 원소로 내림차순 정렬하기
#[(3, 4), (3, 3), (2, 2), (1, 2), (1, -1)]
sort_tuple = sorted(tuple, key=lambda x:x[0], reverse=True)
print(sort_tuple)
#두 번째 원소로 오름차순 정렬하기
#[(1, -1), (2, 2), (1, 2), (3, 3), (3, 4)]
sort_tuple = sorted(tuple, key=lambda x:x[1])
print(sort_tuple)
#첫 번째 원소로 오름차순 정렬하고, 첫 번째 원소가 같은 경우 두 번째 원소로 오름차순 정렬하기
#[(1, -1), (1, 2), (2, 2), (3, 3), (3, 4)]
sort_tuple = sorted(tuple, key=lambda x:(x[0], x[1]))
print(sort_tuple)
#첫 번째 원소로 내림차순 정렬하고, 첫 번째 원소가 같은 경우 두 번째 원소로 오름차순 정렬하기
#[(3, 3), (3, 4), (2, 2), (1, -1), (1, 2)]
sort_tuple = sorted(tuple, key=lambda x:(-x[0],x[1]))
print(sort_tuple)
#------------------------------------------------
dic = {'a': 5, 'x': 2, 'd': 3, 'c': 1, 'f': 1, 'e': 0}
#딕셔너리 키 정렬
#[('a', 5), ('c', 1), ('d', 3), ('e', 0), ('f', 1), ('x', 2)]
sort_dic = sorted(dic.items())
print(sort_dic)
#아이템[1번]으로 내림차순 정렬(-)
#[('a', 5), ('d', 3), ('x', 2), ('c', 1), ('f', 1), ('e', 0)]
sort_dic = sorted(dic.items(), key=lambda x: -x[1])
print(sort_dic)
#아이템[1번]으로 오름차순 정렬(+)
#[('e', 0), ('c', 1), ('f', 1), ('x', 2), ('d', 3), ('a', 5)]
sort_dic = sorted(dic.items(), key=lambda x: x[1])
print(sort_dic)
#아이템[1번]으로 오름차순 정렬 and 같은 경우 키[0번]으로 오름차순 정렬
#[('e', 0), ('c', 1), ('f', 1), ('x', 2), ('d', 3), ('a', 5)]
sort_dic = sorted(dic.items(), key=lambda x: (x[1], x[0]))
print(sort_dic)
#아이템[1번]으로 내림차순 정렬 and 같은 경우 키[0번]으로 내림차순 정렬
#[('e', 0), ('c', 1), ('f', 1), ('x', 2), ('d', 3), ('a', 5)]
sort_dic = sorted(dic.items(), key=lambda x: (-x[1], x[0]))
print(sort_dic)
## sorted의 reverse는 기본적으로 False(오름차순)이기 때문에 reverse(내림차순)
#아이템[1번]으로 오름차순 정렬 and 같은 경우 키[0번]으로 오름차순 정렬
sort_dic = sorted(dic.items(), key=lambda x: (x[1], x[0]), reverse=True)
print(sort_dic)
dicList = [
{'point':90, 'penalty': 60, 'name' : 'kitti' },
{'point':87, 'penalty': 58, 'name' : 'kate' },
{'point':92, 'penalty': 74, 'name' : 'kevin' },
{'point':90, 'penalty': 54, 'name' : 'tison' },
{'point':85, 'penalty': 75, 'name' : 'json' },
]
# point 높은순, penalty 낮은순
sort_dic = sorted(dicList, key=lambda x: (-x['point'], x['penalty']))
print(sort_dic)
# 결과 : [{'point': 92, 'penalty': 74, 'name': 'kevin'}, {'point': 90, 'penalty': 54, 'name': 'tison'}, {'point': 90, 'penalty': 60, 'name': 'kitti'}, {'point': 87, 'penalty': 58, 'name': 'kate'}, {'point': 85, 'penalty': 75, 'name': 'json'}]
#길이, 사전순으로 정렬
words = ['a', 'a', 'a', 'on', 'to', 'has','the', 'the', 'that','very', 'right', 'tuple', 'assignment', 'assignment', 'assignment']
li_sort = sorted(words, key=lambda x: (len(x), x))
print(li_sort)
# 결과 : ['a', 'a', 'a', 'on', 'to', 'has', 'the', 'the', 'that', 'very', 'right', 'tuple', 'assignment', 'assignment', 'assignment']
'코테풀이 > 코테용 파이썬 문법' 카테고리의 다른 글
문자열_정규식_정규식패턴 (0) | 2022.07.01 |
---|---|
문자열_알파벳_한글_숫자_특수문자_구분 (0) | 2022.07.01 |
0_사용_import (0) | 2022.07.01 |
[파이썬] 딕셔너리 key : value를 사용하자 (0) | 2022.03.29 |
리스트를 한줄로 초기화 시키는 법 (0) | 2022.03.23 |
람다_딕셔너리_튜플_정렬