내 Road 개체 목록에서 항목을 삭제하지 않는 이유는 무엇입니까?내 도로 목록에서 도로를 삭제할 수없는 이유는 무엇입니까?
도로 객체 (자기, city1, 시티 2, 길이) 가지고 가고,시 (자기, 이름, 인구) 걸릴 객체 관련 정보;
나는 이러한 개체를 _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
전체 수업을들을 수 있습니까? –
"무향 그래프의 가장자리를 지우는 법"이라는 말을 다시 할 수 있습니다. – asheeshr
깊게 중첩 된'if' 블록은 중복 된 것으로 보입니다. 그들은 j.city1 == x.name 또는 j.city2 == x.name :'절에 함께있을 수 있습니다. –