항공기 종류별 개수 세기
이번 연습에서는 여러 상용 여객기의 엔진 종류가 담긴 dictionary aircraft_engines가 주어집니다. 이 데이터셋에서 엔진 종류별로 항공기가 몇 대인지 세려고 해요. for 루프와 if/else 문을 사용해, 새로운 엔진 종류가 나올 때마다 counts 변수에 1을 더하는 코드를 작성해 보세요.
이 연습은 강의의 일부입니다
MATLAB 사용자를 위한 Python
연습 안내
airplane_types의 각 키:값 쌍을 순회하는 for 루프를 작성하세요.- 각 반복에서 x에는 항공기 이름, y에는 엔진 종류를 넣어 "The x airplane has y engines."를 출력하세요.
- 각 반복마다
countsdictionary에서 해당 engine_type 키의 값을 1씩 증가시키세요. - 마지막으로 "counts" dictionary를 출력하세요
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
counts = {}
# Loop over the key:value pairs in airplane_types
for airplane, engine_type in ____.items():
# Print the aircraft name and engine type of each aircraft
print("The {} airplane has {} engines.".format(____, ____))
# Increment the values of the engine_type key in the counts dictionary
if engine_type in counts:
counts[engine_type] = ____[engine_type] + 1
else:
counts[engine_type] = 1
print(____)