【问题标题】:The order of list is not as expected列表顺序不符合预期
【发布时间】:2015-07-18 18:03:30
【问题描述】:

我有一个列表node_list

In [1]: node_list
Out[1]: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 1, 2, 3, 4, 5, 6]

我从node_listNetworkXG 添加节点

In [2]: import networkx as nx

In [3]: G = nx.Graph()

In [4]: G.add_nodes_from(node_list)

但是当我得到节点列表时,模式改变了!

In [5]: list(G.nodes())
Out[5]: ['a', 1, 'c', 'b', 'e', 'd', 'g', 'f', 'i', 'h', 'j', 2, 3, 4, 6, 5]

我希望Out[5]node_list 具有相同的模式,但这并没有发生。怎么办?

【问题讨论】:

    标签: python graph networkx


    【解决方案1】:

    From the docstringNetworkX.Graph

    Examples
    --------
    Create a graph object that tracks the order nodes are added.
    >>> from collections import OrderedDict
    >>> class OrderedNodeGraph(nx.Graph):
    ...     node_dict_factory=OrderedDict
    >>> G=OrderedNodeGraph()
    >>> G.add_nodes_from( (2,1) )
    >>> G.nodes()
    [2, 1]
    >>> G.add_edges_from( ((2,2), (2,1), (1,1)) )
    >>> G.edges()
    [(2, 1), (2, 2), (1, 1)]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-20
      • 1970-01-01
      相关资源
      最近更新 更多