python - 在 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)
!!!编辑:因此该程序显示有向图。迭代器应该枚举图的所有边。迭代顺序由您决定。
解决方案
推荐阅读
- java - 如何在 Eclipse 中为我的 java 应用程序创建 .keystore 文件
- reactjs - 如何在功能组件内成功调度 redux 后检索数据
- kubernetes - 检测 pod 是否使用任何类型的本地存储
- c++ - C++中可重用的成员函数
- azure-devops - 为什么我们在 Azure Devops 中使用管道下的环境?并且在创建构建/发布方面是否有任何每月/每年的限制?
- python - 使用 Rich 设置不同的背景颜色
- python - 集群数据的可视化
- r - 在 echarts4r 中删除千位逗号
- anylogic - 限制由 if else 函数选择的托盘架的空间
- android - RecyclerView 中的 BaseAdapter 仅显示一项问题