[英]How to remove tuple from list in a while loop
我有 function 应该确定填充容量为 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,第二个是产品尺寸,第三个是价格
所以我创建了这个 function。 我只有一个问题。 堆叠的第一件事是产品 p8,因此需要从 Dairy_items 中删除该产品,以便 function 不再 select 该值再次被使用。 我用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_product
和productID
。 这会导致您在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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.