티스토리 뷰

공부

[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)

https://sinsomi.tistory.com/entry/%EB%AC%B8%EB%B2%95-Python-%EB%94%95%EC%85%94%EB%84%88%EB%A6%AC-%EC%A0%95%EB%A0%AC%ED%95%98%EA%B8%B0

 

[문법 / 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

'공부' 카테고리의 다른 글

[Linux] nohup python3 app.py > log.txt 2>&1 &  (0) 2020.05.21
[Python] calc for numpy  (0) 2020.05.21
[SpringBatch] Chunk  (0) 2020.05.21
[Python] int to binary  (0) 2020.05.20
[JS] get queryString  (0) 2020.05.18
댓글