📅  最后修改于: 2023-12-03 14:45:54.565000             🧑  作者: Mango
这是一个 Python 程序,用于从给定的一组人中,根据城市名称匹配到对应的人。
people = [
{"name": "张三", "city": "北京", "contact": "zhangsan@example.com"},
{"name": "李四", "city": "上海", "contact": "lisi@example.com"},
{"name": "王五", "city": "北京", "contact": "wangwu@example.com"},
{"name": "赵六", "city": "广州", "contact": "zhaoliu@example.com"},
{"name": "钱七", "city": "深圳", "contact": "qianqi@example.com"}
]
def find_people_by_city(city):
found = False # 用于判断是否有匹配的人
for person in people:
if person["city"] == city:
print(f"姓名:{person['name']},联系方式:{person['contact']}")
found = True
if not found:
print("未找到匹配的人")
# 测试
find_people_by_city("北京")
姓名:张三,联系方式:zhangsan@example.com
姓名:王五,联系方式:wangwu@example.com