[英]next(iter()) is throwing error while creating dataset using tensorflow in python
[英]Python: Detect cycle using next and iter in a diminishing set
我正在关注 Tushar Roy 关于检测有向图中循环的视频。 我理解使用白色、灰色和黑色集来检测循环,但我不明白的主要事情是当我们检查white
集的长度时, next(iter(white))
会知道在哪里由于设置大小已更改,因此请下一步。 文档说iter
为您提供了一个迭代器对象, next
将检索下一个项目。 迭代器每次被评估的要点是不同的吗?
def has_cycle(graph):
white = set()
gray = set()
black = set()
for vertex in graph.all_vertex.values():
white.add(vertex)
while len(white) > 0:
current = next(iter(white))
if dfs(current, white, gray, black) == True:
return True
return False
def dfs(current, white, gray, black):
move_vertex(current, white, gray)
for neighbor in current.adjacent_vertices:
if neighbor in black:
continue
if neighbor in gray:
return True
if dfs(neighbor, white, gray, black) == True:
return True
move_vertex(current, gray, black)
return False
def move_vertex(vertex, source_set, destination_set):
source_set.remove(vertex)
destination_set.add(vertex)
next(iter(white))
只返回集合white
一个元素。 集合不是有序的,因此不能保证选择哪个元素。 您认为迭代器会针对循环的每次迭代进行评估是正确的。
集合随着循环的进行而改变的事实禁止使用for
循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.