我可以迭代同時彈出嗎?爲什麼它會跳過我列表中的某個值?
def process_shipments(shipments_list):
next_day_orders = []
bamboo_shoots = 20
bamboo_leaves = 15
for order in shipments_list:
if order[1] < 20 and order[2] < 15 and bamboo_shoots > 0 and bamboo_leaves > 0:
bamboo_shoots -= order[1]
bamboo_leaves -= order[2]
total_cost = ((5*order[1])+(2*order[2]))
print (order[0], "-", "$",total_cost)
else:
next_day_orders += order
shipments_delete[:]
return next_day_orders
我在問這兩個版本是否可能。我試圖優化效率。
迭代時,您無法安全地從列表中刪除項目。 – BrenBarn
@BrenBarn爲什麼它不適用於凱文?我正在刪除該項目,因爲shipments_list最後需要爲空[] ... – user3362071
plz刪除此重複問題@AshwiniChaudhary – user3362071