728x90
"""
딕셔너리의 키, 값 목록을 받아와 정렬하고 출력할 수 있다.
딕셔너리 -> 리스트처럼 여러 항목을 저장하는 자료형
but 리스트와 달리 키, 값의 쌍으로 이루어짐
dict.keys()
-> key list
dict.values()
-> value list
dict.items()
-> (key,value) list
"""
#딕셔너리의 키, 값 모두 리스트 형태로 가져오기
#key list
scores = {'kor':100,'eng':90,'math':80}
print(scores.keys())
#value list
scores = {'kor':100,'eng':90,'math':80}
print(scores.values())
#items list
scores = {'kor':100,'eng':90,'math':80}
print(scores.items())
"""
for문에서 dict key, value 사용
- key
for i in dict.keys():
print(i)
- value
for i in dict.vaues():
print(i)
- key, value
for i in dict.items():
print(i)
- k = key, v = value
for k,v in dict.items():
print(k,v)
"""
#for문으로 dictionary 출력하기
#key
scores = {'kor':100,'eng':90,'math':80}
for i in scores.keys():
print(i)
print()
#value
scores = {'kor':100,'eng':90,'math':80}
for i in scores.values():
print(i)
print()
#items
scores = {'kor':100,'eng':90,'math':80}
for i in scores.items():
print(i)
print()
#k,v
scores = {'kor':100,'eng':90,'math':80}
for k,v in scores.items():
print(k,v)
"""
딕셔너리 정렬하기
-key sort
sorted(dict.keys())
-value sort
sorted(dict.values())
-item sort
sorted(dict.items())
"""
#key
scores = {'kor':100,'eng':90,'math':80}
print(sorted(scores.keys()))
print()
#value
scores = {'kor':100,'eng':90,'math':80}
print(sorted(scores.values()))
print()
#items
scores = {'kor':100,'eng':90,'math':80}
print(sorted(scores.items()))
'Python' 카테고리의 다른 글
파이썬 기초 18. 함수 (0) | 2023.09.18 |
---|---|
파이썬 기초 17. 딕셔너리 연습 (0) | 2023.09.18 |
파이썬 기초 15. 딕셔너리 수정, 삭제 (0) | 2023.09.18 |
파이썬 기초 14. 딕셔너리를 만들고 값을 가져오기 (0) | 2023.09.18 |
파이썬 기초 13. 리스트 연습 (0) | 2023.09.18 |