为什么 NetworkX 中生成的图从 0 而不是 1 开始?

Why do generated graphs in NetworkX start at 0 and not 1?

我是学习 NetworkX 的新手。当我学习如何通过编码创建网络时,以网络G为例,教科书告诉我使用代码“G.add_nodes_from(1,2,3,4)”。不过说到子网,书上举了个例子。代码说:

K5 = nx.complete_graph(5)
clique = nx.subgraph(K5, (0,1,2))

我的问题是:对于这个网络,为什么节点数是从0开始的?它是 0,1,2,3,4。为什么不是“1,2,3,4,5”?我很困惑。

提前感谢大家的帮助!

这是设计使然。它在 docs:

中提到

Node labels are the integers 0 to n-1.

这也与 python(以及其他一般语言)中的索引一致。 python 列表和其他可索引的迭代器中的第一个索引总是 0.