我有一个这样的元组列表(从带有sqlite3的select语句生成):
itemsAndQtyBought = [('Item no.1', 3), ('Item no.2', 0), ('Item no.3', 3), ('Item no.4', 2), ('Item no.5', 1), ('Item no.6', 9), ('Item no.7', 7)]
名单还在继续。这是一个元组的列表,它有一个产品名称和购买该商品的数量。在
我需要创建另一个元组列表,其中前5个项目的元组列表中有5个项目的购买量最高。在
例如,上面的列表会是这样的:
^{pr2}$有办法吗?在
谢谢你的回答。在
只需使用排序和切片前5项:
输出:
^{pr2}$唯一的缺点是:它会对整个列表进行排序
您可以使用^{} :
输出:
^{pr2}$相关问题 更多 >
编程相关推荐