【问题标题】:TypeError: 'DiGraph' object does not support item assignmentTypeError: \'DiGraph\' 对象不支持项目分配
【发布时间】:2022-08-08 03:29:06
【问题描述】:

语境

在尝试在 networkx 的方向图中设置属性时,不是节点属性而是图属性,我遇到了以下错误:

 File \"/some_path/some_filepy\", line 81, in add_result_to_last_graph
    snn_graphs[-1][\"result\"] = result_per_type
TypeError: \'DiGraph\' object does not support item assignment

我没有很快找到关于如何在the documentation 的networkx 图中存储属性的示例,所以想知道:

问题

如何将属性存储在 nx.DiGraph() 对象中?

    标签: graph attributes networkx


    【解决方案1】:

    这个问题我之前也遇到过,但是我没有找到stackoverflow中networkx图形属性设置的具体解决方案,所以这里有一种将属性存储在nx.DiGraph对象中的方法:

    some_graph=nx.DiGraph()
    some_attribute="pancakes"
    # Store the attribute in the graph:
    some_graph.graph["the_attribute_name"] = some_attribute
    

    所以简而言之,在图中存储属性的语法是:<graphname>.graph

    然后可以使用以下命令打印图形属性:

    print(some_graph.graph["the_attribute_name"])
    

    【讨论】:

      猜你喜欢
      • 2016-07-31
      • 2014-02-18
      • 2017-03-26
      • 2021-08-05
      • 1970-01-01
      • 1970-01-01
      • 2013-08-16
      • 1970-01-01
      相关资源
      最近更新 更多