如何在while循环中从列表中删除元组



我有一个函数,它应该确定填充容量为200的堆栈的最佳方式。要求是:

  • 仍然合适的小物品应该放在第一位
  • 如果两个商品的尺寸相同,价格最高的商品应该放在第一位
capacity = 200
dairy_items=[('p1', 10, 3), ('p2', 13, 5),
('p3', 15, 2), ('p4', 26, 2),
('p5', 18, 6), ('p6', 25, 3),
('p7', 20, 4), ('p8', 10, 5),
('p9', 15, 4), ('p10', 12, 7),
('p11', 19, 3), ('p12', 27, 6),
('p13', 16, 4), ('p14', 23, 5),
('p15', 14, 2), ('p16', 23, 5), 
('p17', 12, 7), ('p18', 11, 3),
('p19', 16, 5), ('p20', 11, 4)]

第一个元组元素是唯一的产品id,第二个是产品大小,第三个是价格

所以我创建了这个函数。我只有一个问题。堆叠的第一件事是产品p8,因此需要从dairy_items中删除该产品,这样函数就不会在已经使用时再次选择该值。我用dairy_items.remove((尝试过这个,但它不起作用。有没有其他方法我可以使用,或者我只是没有以正确的方式使用它。

def shelving(dairy_items):
filled = 0
worth = 0
shelves = []
while filled <= capacity:
smallest_product = math.inf
most_expensive_product = 0

for productNr in range(len(dairy_items)):
if dairy_items[productNr][1] < smallest_product:
smallest_product = dairy_items[productNr][1]
most_expensive_product = dairy_items[productNr][2]
productID = dairy_items[productNr][0]

elif smallest_product == dairy_items[productNr][1]:
if dairy_items[productNr][2] > most_expensive_product:
smallest_product = dairy_items[productNr][1]
productID = dairy_items[productNr][0]
filled = filled + smallest_product
worth = worth + most_expensive_product
shelves.append(productID)
dairy_items.remove((productID, smallest_product, most_expensive_product))

return (shelves,filled,worth)

问题是您的elif块没有分配most_expensive_product。因此,您可以在if块中分配一个most_expensive_product,在elif中分配smallest_productproductID。这导致您在remove调用中构造一个元组,该元组最初从未实际存在,因此无法删除。

你可以通过在elif中添加一个赋值来解决这个问题——假设这是你的欲望行为。

most_expensive_product = dairy_items[productNr][2]

我对你的变量名称有点困惑,因为most_expensive_product似乎不是最贵的产品,而是最小产品的价格(或者如果有多个最小的产品,则更贵(。

如果我对你要做的事情的假设是正确的,那么最好使用for循环,这样你就可以避免在索引和重建元组方面遇到麻烦。例如,以下内容似乎符合您的意图:

def shelving(dairy_items):
filled = 0
worth = 0
shelves = []
while filled <= capacity:
chosen_product = dairy_items[0]
for product in dairy_items:
if product[1] < chosen_product[1]:
chosen_product = product
elif chosen_product[1] == product[1]:
if product[2] > chosen_product[2]:
chosen_product = product
filled = filled + chosen_product[1]
worth = worth + chosen_product[2]
shelves.append(chosen_product[0])
dairy_items.remove(chosen_product)
return (shelves, filled, worth)

然而,这仍然有一个问题,您的原始代码有;它只会在循环开始时检查容量,因此会将机框装满。这可以通过在末尾添加一个检查来解决,如下所示:

def shelving(dairy_items):
filled = 0
worth = 0
shelves = []
while filled <= capacity:
chosen_product = dairy_items[0]
for product in dairy_items:
if product[1] < chosen_product[1]:
chosen_product = product
elif chosen_product[1] == product[1]:
if product[2] > chosen_product[2]:
chosen_product = product
if filled + chosen_product[1] <= capacity:
filled = filled + chosen_product[1]
worth = worth + chosen_product[2]
shelves.append(chosen_product[0])
dairy_items.remove(chosen_product)
else:
break
return (shelves, filled, worth)

我的代码中有一些愚蠢的错误。我改变了一些事情,下面的事情对我来说很成功。

def shelving(dairy_items):
filled = 0
worth = 0
shelves = []
while filled <= capacity:
smallest_product = math.inf
most_expensive_product = 0
for productNr in range(len(dairy_items)):
if dairy_items[productNr][1] < smallest_product:
smallest_product = dairy_items[productNr][1]
most_expensive_product = dairy_items[productNr][2]
productID = dairy_items[productNr][0]
elif smallest_product == dairy_items[productNr][1]:
if dairy_items[productNr][2] > most_expensive_product:
smallest_product = dairy_items[productNr][1]
productID = dairy_items[productNr][0]
most_expensive_product = dairy_items[productNr][2]
dairy_items.remove((productID, smallest_product, most_expensive_product))
if filled + smallest_product >= 200:
break
filled = filled + smallest_product
worth = worth + most_expensive_product
shelves.append(productID)


return (shelves, filled , worth)

最新更新