首页 > 解决方案 > 在 Python 中构建迭代器

问题描述

我正在尝试使用 python 构建一个迭代器,但我应该使用一个真正的迭代器,例如使用产量。我只是 python 的初学者,所以我将不胜感激。

class Graph(object):
        def __init__(self):
            #structure for edges and vertices
            self.__vertex_edge_map = {}

        def add_vertex(self,vertex):
            #add new vertex
            if vertex not in self.__vertex_edge_map:
                self.__vertex_edge_map[vertex] = []

        def add_edge(self,source,destination):
            #add edge 
            self.add_vertex(source)
            self.add_vertex(destination)
            if destination not in self.__vertex_edge_map[source]:
                self.__vertex_edge_map[source].append(destination)

!!!编辑:因此该程序显示有向图。迭代器应该枚举图的所有边。迭代顺序由您决定。

标签: pythoniterator

解决方案


推荐阅读