Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用字典使图形类迭代_Python_Python 2.7_Dictionary_Iterator - Fatal编程技术网

Python 使用字典使图形类迭代

Python 使用字典使图形类迭代,python,python-2.7,dictionary,iterator,Python,Python 2.7,Dictionary,Iterator,我正在尝试用Python2.7构建一个iterable graph类。我希望能够遍历包含顶点的字典 到目前为止,从中剪切和粘贴已使我感到困惑,但现在我不知道如何使这项工作如此有效 我可以测试顶点是否存在顶点,如果不存在,可以添加顶点。这部分可能是不必要的。 “if(a不在gra中):”因为验证是在Graph类本身中完成的 预期输出是顶点作为关键点的字典。实际上,我甚至不确定列表是否是更好的使用对象 class Vertex(object): def __init__(self, n):

我正在尝试用Python2.7构建一个iterable graph类。我希望能够遍历包含顶点的字典

到目前为止,从中剪切和粘贴已使我感到困惑,但现在我不知道如何使这项工作如此有效 我可以测试顶点是否存在顶点,如果不存在,可以添加顶点。这部分可能是不必要的。 “if(a不在gra中):”因为验证是在Graph类本身中完成的

预期输出是顶点作为关键点的字典。实际上,我甚至不确定列表是否是更好的使用对象

class Vertex(object):
    def __init__(self, n):
        self.name = n
        self.neighbors = list()

        self.discovery = 0
        self.finish = 0
        self.color = 'black'

    def add_neighbor(self, v):
        if v not in self.neighbors:
            self.neighbors.append(v)
            self.neighbors.sort()


class Graph(object):
    def __init__(self,size):
        self.vertices = {}
        self.hops = 0
        self.count = 0
        self.limit = size

    def __iter__(self):
        return self

    def next(self):
        self.count += 1
        if self.count > self.limit:
            raise StopIteration


    def add_vertex(self,vertex):
        if isinstance(vertex, Vertex) and vertex.name not in self.vertices:
            self.vertices[vertex.name] = vertex
            return True
        else:
            return False

    def add_edge(u,v):
        if u in self.vertices and v in self.vertices:
            for key, value in self.vertices.items():
                if key == u:
                    value.add_neighbor(v)
                if key == v:
                    value.add_neighbor(u)
                    return True
                else:
                    return False



    def _dfs(self, vertex):
            global hops
            vertex.color = 'red'
            vertex.discovery = hops
            hops += 1
            for v in vertex.neighbors:
                if self.vertices[v].color == 'black':
                    self._dfs(self.vertices[v])
            vertex.color = 'blue'
            vertex.finish = hops
            time += 1






input = ((5,3),(4 ,2),(0,1),(2 3),(0 4))

N,l = input[0]
print "N is " + str(N)
print "l is " + str(l)

gra = Graph(N)

for i in xrange(1,l):
    a,b = input[i]
    # Store a and b as vertices in graph object
    print "a is " + str(a) +  " b is " + str(b) 
    if (a not in gra ):
        print "adding a"
        gra.add_vertex(Vertex(chr(a)))
    if (b not in gra ):
        print "adding b"
        gra.add_vertex(Vertex(chr(b)))

您正在尝试使用
不在
中,这将测试包含性;实施以下措施:

def __contains__(self, vertex):
    return vertex.name in self.vertices
我假设您想要测试顶点,所以在测试包容之前创建一个顶点:

a = Vertex(chr(a))
if a not in gra:
    print "adding a"
    gra.add_vertex(a)
对于迭代,我不会让
图形本身成为迭代器;这就限制了您只能迭代一次。您的
next()
方法也缺少
return
语句,因此您所做的就是生成一系列
None
对象

改为将其设置为iterable,因此每次调用
\uuuu iter\uuuu
时都返回一个新的迭代器对象。您可以通过将
\uuuuu iter\uuuu
设置为:


注意
产量
。我假设您希望迭代顶点。

包容测试和迭代是两件独立的事情。您的
next()
方法在迭代时也不会产生任何结果。
def __iter__(self):
    for vertex in self.vertices.itervalues():
        yield vertex