코딩 연습/Python

딕셔너리 정렬하기

썬2 2022. 1. 1. 22:19
dict = {}

for x in letter:
    if x in dict.keys():
        dict[x] += 1
    else:
        dict[x] = 1
        
## 정렬부분
dict = sorted(dict.items(), key=lambda x: x[1], reverse=True)

if len(dict)>=2 and dict[1][1] == dict[0][1]:
    print("?")
else:
    print(dict[0][0])

 

dict.items()후 key에 x[1]하면 value를 기준으로 정렬된다. (x[0]이면 key 기준 정렬)

dict.values() 정렬, dict.keys() 정렬할수도 있다.

'코딩 연습 > Python' 카테고리의 다른 글

백준_11508: 2+1 세일  (0) 2022.02.03
백준_11657: 타임머신  (0) 2022.01.30
Inflearn 다시  (0) 2021.12.19
모의고사  (0) 2021.12.08
가장 큰 수  (0) 2021.12.08