공부
[Python] sort dictionary
승가비
2020. 5. 21. 00:35
728x90
import operator
dict = {'A':1, 'D':4, 'C':3, 'B':2}
# key
sort_dict = sorted(dict.items())
print(sort_dict)
# value
sort_dict = sorted(dict.items(), key=lambda x:x[1])
print(sort_dict)
[문법 / Python] 딕셔너리 정렬하기
딕셔너리를 정렬하는 방법들을 알아보겠습니다. 딕셔너리는 리스트처럼 sort()함수를 사용할 수 없기때문에 sorted()를 이용해서 정렬해야 합니다. KEY를 이용한 정렬 import operator dict = {'A':1, 'D':4, 'C
sinsomi.tistory.com
https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value
How do I sort a dictionary by value?
I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary. I can sort on the keys, but ...
stackoverflow.com
728x90