pyton dictionary의 sorted()를 이해한다.
수정, 개선이 필요할 경우 지적해주시면 감사하겠습니다.
# 초기 dictionary 설정
my_dic = {
"Apple" : "철수",
"Dried_persimmon" : "봉수",
"Carot" : "민정",
"Banana" : "영희",
}
'''
Sorted of dictionary key
>> sorted()함수 이용
'''
# sorted()함수는 no-inplace!! **반면, sort는 inplace임을 기억하자**
# 따라서 sorted()함수는 my_dic에 영향을 주지 않기 때문에 변수에 넣어 사용해주자.
key_sorted = sorted(my_dic.items())
print(f"dictionary key 정렬 : ", key_sorted)
'''
Sorted of dictionary value
>> operator 내장 함수 이용
'''
import operator
# dictionary value로 정렬
# ** operator.itemgetter(0) : key sorted
# > key_sorted = sorted(my_dic.items()와 동일한 값 출력
# ** operator.itemgetter(1) : value sorted
# > dictionary value로 sorted
value_sorted = sorted(my_dic.itmes(), key = operator.itemgetter(1))
print(f"dictionary value 정렬 : ", value_sorted)
