如何在python上对产品和价格进行排名

时间:2018-06-27 23:13:54

标签: python sorting

我正在尝试将杂货清单放入python,然后以最低的价格对它们进行排名。有谁知道我怎么显示这个,结果看起来像这样:

Popcorn: 1.23
Eggs: 3.77
Water: 4.34
Milk: 7.12
Toast: 8.45

这是我到目前为止尝试过的

price_list = [["Eggs", "3.77"], ["Toast", "8.45"], ["Milk", "7.12"], ["Water", "4.34"], ["Popcorn", "1.23"]]
print("Original order: ", price_list)
price_list.sort()
print("Sorted Order: ", price_list)

1 个答案:

答案 0 :(得分:1)

price_list = [["Eggs", "3.77"], ["Toast", "8.45"], ["Milk", "7.12"], ["Water", "4.34"], ["Popcorn", "1.23"]]
print("Original order: ", price_list)
print("Sorted Order: ", sorted(price_list, key=lambda x: float(x[1])))