louvain_partitions generator issue

10 views
Skip to first unread message

Michael

unread,
Jul 27, 2022, 12:37:57 PMJul 27
to networkx-discuss
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

Dan Schult

unread,
Jul 27, 2022, 12:54:33 PMJul 27
to networkx...@googlegroups.com
Something is wrong here... perhaps to do with yield and return being used in the same function.
Thanks!
Reply all
Reply to author
Forward
0 new messages