价值观没有通过

时间:2015-10-09 20:06:23

标签: python list class methods

我的以下课程功能

    def add_vertex(self,vertex):   
        for i in range(self.num_vertices):
            if self.vertices[i] == vertex:
                return

        self.vertices[self.num_vertices]=vertex
        for i in range(self.num_vertices):
            self.edges[self.num_vertices][i]=0
            self.edges[i][self.num_vertices]=0
        self.num_vertices +=1


    def add_edges(self,from_vertex,to_vertex,weight):
        row=self.index_is(from_vertex)
        col=self.index_is(to_vertex)
        self.edges[row][col]=weight

当我把这些值放在

i=0
while i<len(to):
    my_graph.add_vertex(from_[i])
    my_graph.add_edges(from_[i],to[i],distance[i])
    i=i+1

但价值并未过去 从_ [],到[]和距离[]

的值
['Luebeck', 'Hamburg', 'Hamburg', 'Hamburg', 'Bremen', 'Bremen', 'Hannover', 'Hannover', 'Magdeburg', 'Berlin', 'Dresden', 'Leipzig', 'Dortmund', 'Kassel', 'Frankfurt', 'Frankfurt', 'Leipzig', 'Dortmund', 'Saarbruecken', 'Saarbruecken', 'Karlsruhe', 'Stuttgart', 'Stuttgart', 'Stuttgart', 'Nuremberg', 'Manchester', 'Birmingham', 'Birmingham']

['Hamburg', 'Bremen', 'Hannover', 'Berlin', 'Hannover', 'Dortmund', 'Magdeburg', 'Kassel', 'Berlin', 'Dresden', 'Leipzig', 'Magdeburg', 'Duesseldorf', 'Frankfurt', 'Dortmund', 'Nuremberg', 'Nuremberg', 'Saarbruecken', 'Frankfurt', 'Karlsruhe', 'Stuttgart', 'Frankfurt', 'Munich', 'Nuremberg', 'Munich', 'Birmingham', 'Bristol', 'London']

['63', '116', '153', '291', '132', '234', '148', '165', '166', '204', '119', '125', '69', '185', '221', '222', '263', '350', '177', '143', '71', '200', '215', '207', '171', '84', '85', '117']

1 个答案:

答案 0 :(得分:0)

其他问题(提示:查看枚举的工作方式),   你的代码不会像写的那样工作,因为你只是添加了顶点,所以当需要查找内容时,你会得到一个错误(此外,即使某些内容也来自于,你会遇到一个问题,你在添加顶点之前尝试添加边缘。保留大部分代码的最佳方法是改变你的数据输入,而不是像:

for i in xrange(len(from_):
    g.add_vertex(from_[i])
for i in xrange(len(to):
    g.add_vertex(to[i])

for i in xrange(min(len(_from),len(to),len(weight)))
    g.add_edges(_from[i],to[i],weight[i])

(min只是为了确保你的3个列表长度相同)