统计不同类型的飞机
在本练习中,给出了一个字典 aircraft_engines,其中包含多款常见商用飞机的发动机类型。您的目标是统计数据集中每种发动机类型对应的飞机数量。请运用 Python 的控制流,编写一个包含 if/else 语句的 for 循环:对遇到的每一种新的发动机类型,将 counts 变量加 1。
本练习是课程的一部分
面向 MATLAB 用户的 Python
练习说明
- 编写一个 for 循环,遍历
airplane_types的每个 key:value 对。 - 每次迭代时,打印 "The x airplane has y engines.",其中 x 是飞机名称,y 是发动机类型。
- 每次迭代时,递增
counts字典中对应 engine_type 键的值。 - 最后,打印 "counts" 字典。
交互式实操练习
通过完成这段示例代码来试试这个练习。
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(____)