반응형
    
    
    
  List Dictionary sort multiple
딕셔너리 리스트를 여러개의 key 로 정렬 시키기
sorted 와 lambda 식을 사용하여 멀티 정렬이 가능합니다.
아래의 lambda 식에서
e:( -e['point'], e['penalty'])
를 보면, 내림차순은 - 를 붙혀주면 됩니다.
아래 예제는 point 가 높은 순, point가 같다면 penalty 가 낮은 순으로 정렬하는 예제입니다.
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 낮은순
sorted_arr = sorted(dicList, key=lambda e: (-e['point'], e['penalty']))
>>> sorted_arr 
>>> {'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' },
728x90
    
    
  반응형
    
    
    
  'Python' 카테고리의 다른 글
| [python] Dictionary 의 특정 값의 sum, max, min 값 가져오기 (0) | 2019.02.15 | 
|---|---|
| [python] console 에서 Ctrl+C 로 종료 시 에러메시지 안뜨게 하기 (0) | 2019.02.15 | 
| 파일 쓰기 file write read append (0) | 2019.02.14 | 
| python 오늘날짜, 현재시간 now (0) | 2019.02.13 | 
| python dictionary 에서 max 값 가져오기 (key or value) (0) | 2019.02.13 | 
 
										
									 
										
									 
										
									
댓글