![](/img/trans.png)
[英]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.