2012-11-29 82 views
1

爲什麼不會從道路對象列表中刪除項目?爲什麼我不能從道路清單中刪除道路?

相關信息

道對象需要(自我,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 
+1

你可以放在全班上課嗎? –

+0

您可以將該問題改爲「如何刪除無向圖的邊緣」。 – asheeshr

+0

深嵌「if」塊看起來多餘;他們可以在一個'if j.city1 == x.name或j.city2 == x.name:'子句 –

回答

2

原因可能是您刪除了迭代的列表元素。 這通常是一種不好的做法。

+1

雖然這通常是一個不好的做法,他說道路被刪除。在嘗試拆除城市之前,道路將被刪除。 –

+0

Vkontori,我查了一下,我認爲@Joshua D. Boyd是對的 - 我沒有看到我會做什麼(除非你這樣做?) –

+0

你的bug在hasRoad中不會檢查所有道路,而是它如果第一個不匹配,將返回false。 – vkontori

0

delCity可以改進,但它似乎工作正常。我相信你需要發佈更多的代碼。我編寫了示例代碼來測試delCity。這裏是城市類:

class City(object): 
    def __init__(self, name, population): 
     self.name = name 
     self.population = population 

    def __repr__(self): 
     return "City(%r, %r)" % (self.name, self.population) 

這裏是道班:

class Road(object): 
    def __init__ (self, city1, city2, length): 
     self.city1 = city1 
     self.city2 = city2 
     self.length = length 

    def __repr__(self): 
     return "Road(%r, %r, %r)" % (self.city1, self.city2, self.length) 

這裏是我貼你delCity方法到測試類:

class Test(object): 
    def __init__(self, cities, roads): 
     self._cities = cities 
     self._roads = roads 

    def hasCity(self, city_name): 
     for c in self._cities: 
      if c.name == city_name: 
       return True 
     return False 

    def delRoad(self, pair): 
     for x in self._roads: 
      if x.city1 == pair[0] and x.city2 == pair[1]: 
       self._roads.remove(x) 

    def delCity(self, city): 
     if self.hasCity(city) == False: #checks to see if city isn't in list 
      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)) ##delRoad takes a tuple 
          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 

在這裏,我現在測試你的代碼:

>>> t = Test([City('x', 1), City('y', 1), City('z', 1)], 
      [Road('x', 'y', 1), Road('y', 'z', 1)]) 
>>> t.delCity('x') 

>>> print t._cities 
[City('y', 1), City('z', 1)] 
>>> print t._roads 
[Road('y', 'z', 1)] 

如您所見,去往該城市的城市和單路已被刪除。

+0

嗯,我會繼續調試。感謝您爲我檢查。當我再次將我的頭撞到牆上時,我會回來。 –

相關問題