创建宠物寄养记录数据库,用字典对象保存寄养的宠物信息,包括宠物编号、宠物名称、品种名称、寄养天数、主人称呼、主人嘱咐等。要求支持下面的查询: “有多少只猫” “寄养天数大于15的宠物” “主人嘱咐中出现‘脾气不好’的宠物”
时间: 2024-11-13 19:26:28 浏览: 16
创建宠物寄养记录数据库,我们可以使用Python的字典数据结构来存储每个宠物的信息。例如:
```python
pet_records = [
{
"pet_id": 001,
"pet_name": "小花",
"breed": "暹罗猫",
"boarding_days": 20,
"owner": "张三",
"instructions": "注意它脾气不太好",
},
{
"pet_id": 002,
"pet_name": "小白",
"breed": "金毛",
"boarding_days": 18,
"owner": "李四",
"instructions": "每天记得喂食",
},
# 更多宠物记录...
]
```
对于查询功能,我们可以编写一些函数来进行处理:
1. 查询有多少只猫:
```python
def count_cats():
cats = [record for record in pet_records if record["breed"] == "猫"]
return len(cats)
```
2. 寄养天数大于15的宠物:
```python
def pets_more_than_15_days():
result = [record for record in pet_records if record["boarding_days"] > 15]
return result
```
3. 主人嘱咐中包含“脾气不好”的宠物:
```python
def pets_with_instruction("脾气不好"):
result = [record for record in pet_records if "脾气不好" in record["instructions"]]
return result
```
阅读全文