728x90
반응형
안녕하세요. 스마트팩토리입니다.
파이썬 중급 #109 collections 모듈 -deque(데크) 모듈-ㅡOrderedDict()에 대해 알아보겠습니다.
1. OrderedDict()
OrderedDict()
- 딕셔너리의 순서를 고려한 모듈
dic={}
dic["b"]=200
dic["a"]=100
dic["c"]=300
dic["d"]=400
dic["f"]=600
dic["e"]=500
dic["g"]=700
for k, v in dic.items():
print(k,v)
일반적인 딕셔너리는 순서를 고려하지 않고 출력이 됩니다. 하지만 버전 업이 된 파이썬의 경우 마치 순서가 있는것 즉 인덱스가 있는 듯이 출력되는 것을 볼 수 있습니다. 하지만 이는 인덱스가 있는 것이 아니라는 것을 알아야 합니다.
print("~~~~~~~~~~~~~~~")
from collections import OrderedDict
dic=OrderedDict()
dic["b"]=200
dic["a"]=100
dic["c"]=300
dic["d"]=400
dic["f"]=600
dic["e"]=500
dic["g"]=700
for k, v in dic.items():
print(k,v)
딕셔너리 값의 특성을 고려하여 순서가 있는 것 처럼 출력 해 주는 모듈이 바로 OrderedDict 모듈입니다.
이런 것이 있다는 것을 아는 것이 포인트입니다.
dic={}
dic["b"]=200
dic["a"]=100
dic["c"]=300
dic["d"]=400
dic["f"]=600
dic["e"]=500
dic["g"]=700
for k, v in dic.items():
print(k,v)
print("~~~~~~~~~~~~~~~")
from collections import OrderedDict
dic=OrderedDict()
dic["b"]=200
dic["a"]=100
dic["c"]=300
dic["d"]=400
dic["f"]=600
dic["e"]=500
dic["g"]=700
for k, v in dic.items():
print(k,v)
728x90
반응형
'Python' 카테고리의 다른 글
파이썬 중급 #108 collections 모듈 -deque(데크) 모듈-ㅡmaxlen(),popleft() (0) | 2022.07.28 |
---|---|
파이썬 중급 #107 collections 모듈 -deque(데크) 모듈-extend(), extendleft() (0) | 2022.07.26 |
파이썬 중급 #106 collections 모듈 -deque(데크) 모듈-rotate(n) (0) | 2022.07.25 |
파이썬 중급 #105 collections 모듈 -deque(데크) 모듈-clear(), appendleft() (0) | 2022.07.19 |
파이썬 중급 #104 collections 모듈 -deque(데크) 모듈-append(), pop() (0) | 2022.07.13 |
댓글