Skip to main content
Ctrl+K
NetworkX 3.4rc0.dev0 documentation - Home
  • 安装
  • 教程
  • Reference
  • Gallery
  • Developer
  • 发布版本
  • Guides
  • Home Page
  • GitHub
  • 安装
  • 教程
  • Reference
  • Gallery
  • Developer
  • 发布版本
  • Guides
  • Home Page
  • GitHub

Section Navigation

  • 引言
  • Graph types
  • Algorithms
  • Functions
  • Graph generators
  • Linear algebra
  • Converting to and from other data formats
  • Relabeling nodes
  • Reading and writing graphs
    • Adjacency List
    • Multiline Adjacency List
    • DOT
    • Edge List
    • GEXF
    • GML
    • GraphML
    • JSON
    • LEDA
    • SparseGraph6
    • Pajek
    • Matrix Market
    • Network Text
  • Drawing
  • 随机性
  • Exceptions
  • Utilities
  • 后端和配置
  • 术语表
  • Reference
  • JSON
  • node_link_data

node_link_data#

node_link_data(G, *, source='source', target='target', name='id', key='key', link='links', nodes='nodes')[source]#

返回适合JSON序列化和在JavaScript文档中使用的节点链接格式数据。

Parameters:
GNetworkX图
source字符串

用于存储NetworkX内部图数据的’source’属性名称。

target字符串

用于存储NetworkX内部图数据的’target’属性名称。

name字符串

用于存储NetworkX内部图数据的’name’属性名称。

key字符串

用于存储NetworkX内部图数据的’key’属性名称。

link字符串

用于存储NetworkX内部图数据的’link’属性名称。

nodes字符串

用于存储NetworkX内部图数据的’nodes’属性名称。

Returns:
data字典

包含节点链接格式数据的字典。

Raises:
NetworkXError

如果’source’、’target’和’key’的值不唯一。

See also

node_link_graph, adjacency_data, tree_data

Notes

图、节点和链接属性以这种格式存储。注意,属性键将被转换为字符串以符合JSON规范。

属性’key’仅用于多重图。

要结合使用 node_link_data 和 node_link_graph ,属性的关键字名称必须匹配。

Examples

>>> from pprint import pprint
>>> G = nx.Graph([("A", "B")])
>>> data1 = nx.node_link_data(G)
>>> pprint(data1)
{'directed': False,
 'graph': {},
 'links': [{'source': 'A', 'target': 'B'}],
 'multigraph': False,
 'nodes': [{'id': 'A'}, {'id': 'B'}]}

使用JSON进行序列化

>>> import json
>>> s1 = json.dumps(data1)
>>> s1
'{"directed": false, "multigraph": false, "graph": {}, "nodes": [{"id": "A"}, {"id": "B"}], "links": [{"source": "A", "target": "B"}]}'

图也可以通过传递 node_link_data 作为编码器函数进行序列化。这两种方法是等效的。

>>> s1 = json.dumps(G, default=nx.node_link_data)
>>> s1
'{"directed": false, "multigraph": false, "graph": {}, "nodes": [{"id": "A"}, {"id": "B"}], "links": [{"source": "A", "target": "B"}]}'

存储NetworkX内部图数据的属性名称可以指定为关键字选项。

>>> H = nx.gn_graph(2)
>>> data2 = nx.node_link_data(
...     H, link="edges", source="from", target="to", nodes="vertices"
... )
>>> pprint(data2)
{'directed': True,
 'edges': [{'from': 1, 'to': 0}],
 'graph': {},
 'multigraph': False,
 'vertices': [{'id': 0}, {'id': 1}]}
On this page
  • node_link_data()

© Copyright 2004-2024, NetworkX Developers.

Created using Sphinx 7.4.7.

Built with the PyData Sphinx Theme 0.15.4.

优云智算