ValueError: max() arg is an empty sequence with NetworkX

ValueError: max() arg is an empty sequence with NetworkX

我正在尝试确定网络巨大组件的节点数(大小)。我在 for 循环中执行此操作,在该循环中我评估了一系列外部负载对网络本身的影响 - 这就是为什么您会在下面看到 for 循环。

我的代码如下:

#create the graph G
import networkx as nx
G=nx.grid_2d_graph(N,N) #N=100 and represents the side of a square grid
pos = dict( (n, n) for n in G.nodes() ) #Dictionary of all positions
#do stuff
giant_component=OrderedDict() #Dict with the size of the giant component for each simulation
for i,file in enumerate(os.listdir(directoryPath)):
    if file.endswith(".csv"):
    #do stuff
    giant_component[file]=max(nx.connected_component_subgraphs(G), key=len) 
    #do stuff

但我在第一次迭代时遇到错误:

ValueError                                Traceback (most recent call last)
C:\Users\Francesco\Desktop\Scripts\Network_Scripts\Lattice_ForLoop.py in <module>()
    273         percent_total_active_nodes[file]=100*act_nodes_model/10000                  #Total (Stage 1 + Stage 2)
    274 
--> 275         giant_component[file]=max(nx.connected_component_subgraphs(G), key=len)     #Analysis (Stage 1 + Stage 2)
    276         largest_cc = max(nx.connected_components(G), key=len)
    277         network_diam[file]=len(largest_cc)                                          #Analysis (Stage 1 + Stage 2) = Diameter of the largest component

ValueError: max() arg is an empty sequence 

问题: max() arg is an empty sequencemax(nx.connected_component_subgraphs(G), key=len) 关联时是什么意思?

"What does max() arg is an empty sequence mean when associated with max(nx.connected_component_subgraphs(G), key=len)?"

它的意思就是它所说的:max 的参数是一个空序列。更具体地说 nx.connected_component_subgraphs(G) 是空的。据我所知,唯一会导致这种情况的是 G 没有节点。