小区登记宠物,记下一串种类,比如 ["cat", "dog", "cat"]。写 count_pets(pets),返回一个字典:每种宠物有几只,按第一次出现的顺序。列表是空的就返回 {}。
count_pets(["cat", "dog", "cat"])→{'cat': 2, 'dog': 1}count_pets(["fish", "fish", "fish"])→{'fish': 3}def count_pets(pets):
counts = {}
for pet in pets:
# 在这里写:第一次见到记 1,见过就加 1
pass
return counts
写好了先点「运行」试试示例,没问题再点「提交」。