我有一堂课,有一个叫做tour的领域。在该课程中,我还具有从游览中删除位置的功能。
def remove(self,location, to_print=False):
location.tour_id = None
index_to_delete = None
if to_print:
print("before removee" +str(len(self.tour_locations)))
for i in range(0,len(self.tour_locations)):
if self.tour_locations[i].id_ == location.id_:
index_to_delete = i
if index_to_delete is None:
print("element not found")
return
else:
del self.tour_locations[index_to_delete]
if to_print:
print("after removee" +str(len(self.tour_locations)))
现在我从其他地方调用此函数(不在旅游班上):
def apply(self):
tour_location = self.extra_information['tour_location']
position = self.extra_information['position']
tour= self.extra_information['tour']
origin_tour = self.solution.get_tour_by_id(tour_location.tour_id)
destination_tour = self.solution.get_tour_by_id(tour.id_)
if to_print:
print("origin_tour before " + str(origin_tour))
print("dstination tour before" + str(destination_tour))
origin_tour.remove(tour_location, to_print=to_print)
destination_tour.insert_at(tour_location, position)
if to_print:
print("origin_tour after " + str(origin_tour))
print("dstination tour after" + str(destination_tour))
return self.solution
班级中的打印行正在打印与位置移除一致的游览。
应用功能中的打印行(“之后”)显示游览,没有任何修改。
这是怎么回事?
您不应该使用del
从列表中删除项目。请改用list.pop(index)
或list.remove(value)
。在您的情况下,应使用list.pop(index)
,因为您要从列表中删除特定索引处的项目。 Documentation