为什么我不能从道路列表中删除道路?

时间:2012-11-29 03:45:32

标签: python list graph

为什么不从我的道路对象列表中删除项目?

相关信息

道路对象采取(自我,城市1,城市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

2 个答案:

答案 0 :(得分:2)

原因可能是您删除了迭代列表的元素。 这通常是一种不好的做法。

答案 1 :(得分:0)

delCity可以改进,但似乎工作正常。我相信你需要发布更多代码。我编写了示例代码来测试delCity。这是City类:

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)

这是Road类:

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)]

如您所见,去过该城市的城市和单一道路已被删除。