开始使用免费开始使用

列表排序

您的外卖平台正在扩张,每天都有新餐厅入驻。您有一个包含餐厅名称的 restaurants 列表,以及一个显示平均备餐时间(单位为分钟)的 cooking_times 列表。您的团队需要将这两个列表排序,以便找出表现最佳的餐厅并优化配送调度。

让我们用另一个内置函数来完成这个需求。

本练习是课程的一部分

Python 中级:面向开发者

查看课程

练习说明

  • restaurants 按字母顺序排序,并保存为 restaurants_sorted
  • cooking_times 按从快到慢排序,并保存为 cooking_times_sorted

交互式实操练习

通过完成这段示例代码来试试这个练习。

restaurants = ["Sushi Central", "Burger Hub", "Taco Town", "Pizza Palace"]
cooking_times = [30, 25, 35, 40, 28, 32, 29, 31, 12, 55]

# Sort restaurant names alphabetically
restaurants_sorted = ____(____)

# Sort cooking times from fastest to slowest
cooking_times_sorted = ____(____)

print("Restaurants (A–Z):", restaurants_sorted)
print("Cooking times (ascending):", cooking_times_sorted)
编辑并运行代码