Hi all
I cannot explain the behavior of the following code. Why do I get different results when I iterate with a for loop the generator directly and when I iterate over the list of the generator?
----------------------------
import networkx as nx
import networkx.algorithms.community as nx_comm
G = nx.path_graph(200)
def evaluate(part):
print(len(part))
count = 0
for nodes in part:
if len(nodes) == 0:
count += 1
print(f"\tEmpty Clusters: {count}")
louvian_clustering = list(nx_comm.louvain_partitions(G, resolution=1, seed=20))
for part in louvian_clustering:
evaluate(part)
print("----")
for part in nx_comm.louvain_partitions(G, resolution=1, seed=20):
evaluate(part)
----------------------------
I get the following output:
----------------------------
90
Empty Clusters: 76
41
Empty Clusters: 27
17
Empty Clusters: 3
14
Empty Clusters: 0
----
90
Empty Clusters: 0
41
Empty Clusters: 0
17
Empty Clusters: 0
14
Empty Clusters: 0
----------------------------
Thanks for an explanation.
Michael