这是一个老问题。然而,我仍然想发表一下我的见解。我也遇到了同样的问题。我不确定实际问题的阻碍点是什么,但我会写下我所做的事情。
所以,我想创建一个包含四个节点(56、78、90和112)的完全图。我有一个列表。我查找了
complete_graph
的定义,以下是我看到的内容。
Signature: nx.complete_graph(n, create_using=None)
Docstring:
Return the complete graph `K_n` with n nodes.
Parameters
----------
n : int or iterable container of nodes
If n is an integer, nodes are from range(n).
If n is a container of nodes, those nodes appear in the graph.
create_using : NetworkX graph constructor, optional (default=nx.Graph)
Graph type to create. If graph instance, then cleared before populated.
Examples
--------
>>> G = nx.complete_graph(9)
>>> len(G)
9
>>> G.size()
36
>>> G = nx.complete_graph(range(11, 14))
>>> list(G.nodes())
[11, 12, 13]
>>> G = nx.complete_graph(4, nx.DiGraph())
>>> G.is_directed()
True
这意味着它可以接受一个迭代器。出于同样的精神,我尝试了以下代码:
In [6]: l = [56,78,90,112]
In [7]: G = nx.complete_graph(l)
In [8]: G.edges(data=True)
Out[8]: EdgeDataView([(56, 78, {}), (56, 90, {}), (56, 112, {}), (78, 90, {}), (78, 112, {}), (90, 112, {})])
In [10]: G.nodes(data=True)
Out[10]: NodeDataView({56: {}, 78: {}, 90: {}, 112: {}})
所以,这就是一个由列表构建的完整图。
我希望这回答了这个问题。