'Dictionary' 태그의 글 목록 — 뚝딱이

Dictionary

Python/이론, 기초

[Python] 딕셔너리 정렬하기

Key 기준 정렬 오름차순 dict은 list와 같이 .sort 메소드를 가지고 있지 않아 sorted()를 사용하여 정렬하며 key를 기준으로 정렬된다. .items를 쓰지 않으면 정렬된 key만 list로 출력된다. d = dict(a=3, c=7, b=2, d=1) print(d) s_d = sorted(d.items()) print(s_d) {'a': 3, 'c': 7, 'b': 2, 'd': 1} [('a', 3), ('b', 2), ('c', 7), ('d', 1)] 내림차순 내림차순으로 정렬하기 위해서 reverse = Trur로 설정하면 된다. d = dict(a=3, c=7, b=2, d=1) print(d) s_d = sorted(d.items(), reverse= True) print..

파송송
'Dictionary' 태그의 글 목록