1

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

相关信息

道路对象取(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
4

2 回答 2

2

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

于 2012-11-29T03:56:12.650 回答
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 方法粘贴到的 Test 类:

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

如您所见,前往该城市的城市和单条道路已被删除。

于 2012-11-29T04:11:19.887 回答