코딩테스트[파이썬]/백준 (BOJ)

백준 2092 - 딕셔너리 정렬

softmoca__ 2024. 2. 18. 14:31
목차

https://www.acmicpc.net/problem/20920

 

20920번: 영단어 암기는 괴로워

첫째 줄에는 영어 지문에 나오는 단어의 개수 $N$과 외울 단어의 길이 기준이 되는 $M$이 공백으로 구분되어 주어진다. ($1 \leq N \leq 100\,000$, $1 \leq M \leq 10$) 둘째 줄부터 $N+1$번째 줄까지 외울 단

www.acmicpc.net

 

 

 

 

 

 

import sys
from collections import defaultdict
n,m=map(int,input().split())
dic=defaultdict(int)
for _ in range(n):
    x=sys.stdin.readline().rstrip()
    if len(x)>=m:
        dic[x]+=1

dic2=sorted(dic.items(),key= lambda x: (-x[1],-len(x[0]),x[0]))

for x in dic2:
    print(x[0])

 

.items과 sorted 함수를 사용해서 딕셔너리를 정렬할 수 있다.