为什么这不会从我的道路对象列表中删除项目?
相关信息
道路对象取(self,city1,city2,length),City对象取(self,name,population);
我将这些对象保存到列表 _cities 和 _roads 中,以便我可以修改它们。
此定义应该删除连接到城市的任何道路,然后删除该城市。
但是,我的代码不想删除我的道路(而且我没有收到任何错误),所以我的逻辑一定是有缺陷的。
你能帮我吗?
class Network:
def __init__(self):
self._cities = [] # list of City objects in this network
self._roads = [] # list of Road objects in this network
def hasCity(self, name):
for x in self._cities:
if x.name == name:
return True
return False
def hasRoad(self, road):
for x in self._roads:
if x.city1 == road[0] and x.city2 == road[1]:
return True
elif x.city1 == road[1] and x.city2 == road[0]:
return True
else:
return False
def addCity(self, name, pop):
if self.hasCity(name) == True:
return False
else:
self._cities.append(City(name, pop))
return True
def addRoad(self, road, length):
if self.hasRoad(road) == True:
return False
else:
self._roads.append(Road(road[0], road[1], length))
return True
def delRoad(self, road):
if self.hasRoad(road) == False:
return False
else:
for x in self._roads:
if x.city1 == road[0] and x.city2 == road[1]:
self._roads.remove(x)
return True
elif x.city1 == road[1] and x.city2 == road[0]:
self._roads.remove(x)
return True
else:
return False
def delCity(self, city):
if self.hasCity(city) == False:
return False
else:
for x in self._cities:
if x.name == city:
for j in self._roads:
if j.city1 == x.name:
self.delRoad((j.city1, j.city2))
self.delRoad((j.city2, j.city1))
elif j.city2 == x.name:
self.delRoad((j.city1, j.city2))
self.delRoad((j.city2, j.city1))
self._cities.remove(x)
return True