상세 컨텐츠

본문 제목

dictionary + iteration 딕셔너리와 반복문

Programming/Python

by 쌩우 2019. 7. 22. 18:52

본문

dictionary는 반복문에서 순서대로 불러오지 않는다!!!

순서가 중요하면 list를 사용하자!!!

for in dictionary.keys() | for in dictionary.values()

ages = {'Tod':35, 'Jane':21, 'John': 27}

for key in ages.keys():
    print(key)    # 'Tod', 'Jane', 'John'

for value in ages.values():
    print(value) # 35, 21, 27

for key in ages.keys():
    print('{}의 나이는 {}입니다').format(key, ages[key])

 for key in ages:
     print('{}의 나이는 {}입니다').format(key, ages[key])    # 위와 동일한 결과가 나온다

for key, value in dictionary.items():

list의 enumerate 같은 기능을 하는 method이다

fruits = {'apple': 1, 'banana': 3, 'plum': 5}

for fruit, numbers in fruits.items():
    print('{}의 개수는 {}입니다').format(fruit, numbers)

'Programming > Python' 카테고리의 다른 글

AWS EC2에 pyenv로 python 설치하기  (0) 2020.07.01
Django - intro  (0) 2019.07.23
PIP와 Virtualenv  (0) 2019.07.22
자료구조 - dictionary  (0) 2019.07.21
모듈 만들기  (0) 2019.07.21

관련글 더보기

댓글 영역