Kodewick
开始学 →
基础←→0 / 80

便宜的先买

#002基础排序列表约 4 分钟

购物清单里每一样是 (名字, 价格)。写 cheapest_first(items),按价格从低到高排好,返回名字的列表。价格都不一样。

示例
cheapest_first([("tea", 12), ("cake", 25), ("water", 3)])→['water', 'tea', 'cake']
cheapest_first([])→[]
main.py
def cheapest_first(items):
    # 在这里写:用 sorted 和 key,按价格(第 2 个)排
    ordered = items
    return [name for name, price in ordered]
Ctrl + Enter 也能运行
结果

写好了先点「运行」试试示例,没问题再点「提交」。