더 파이썬답게 딕셔너리 다루기
지금까지는 딕셔너리의 키를 많이 사용해 데이터를 접근했지만,
Python에서 딕셔너리를 순회할 때 선호되는 방법은
.items() 메서드를 사용하는 것입니다.
이 메서드는 딕셔너리의 각 키와 값을 튜플로 반환하며, for 루프에서 이를 언패킹할 수 있어요. 이제 이를 연습해 보겠습니다.
squirrels_by_park 딕셔너리를 불러왔고, Madison Square Park 키에는 딕셔너리의 리스트가 들어 있습니다.
이 연습은 강의의 일부입니다
Python의 데이터 타입
연습 안내
squirrels_by_park["Madison Square Park"]의 첫 번째 레코드를 순회하면서, 그 항목들을field와value로 언패킹하세요.- 각
field와value를 출력하세요.
- 각
- 같은 과정을
squirrels_by_park["Union Square Park"]의 두 번째 레코드에도 적용하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Iterate over the first squirrel entry in the Madison Square Park list
for ____, ____ in ____["____"][____].____():
# Print field and value
print(____, ____)
print('-' * 13)
# Iterate over the second squirrel entry in the Union Square Park list
____
# Print field and value
____