parse_edgelist#

parse_edgelist(lines, comments='#', delimiter=None, create_using=None, nodetype=None, data=True)[源代码]#

解析图的边列表表示形式的行。

参数
lines字符串的列表或迭代器

以边缘列表格式输入数据

comments字符串,可选

注释行的标记。默认值为 '#' 。若要指定不应将任何字符视为注释,请使用 comments=None

delimiter字符串,可选

节点标签的分隔符。默认值为 None ,表示任何空格。

create_usingNetworkX图形构造函数,可选(默认=nx.Graph)

要创建的图表类型。如果是图表实例,则在填充之前清除。

nodetypePython类型,可选

将节点转换为此类型。默认值为 None ,表示不执行任何转换。

data布尔或(标签、类型)元组列表

如果 False 不生成边缘数据或如果 True 使用边数据的字典表示或指定边数据的字典关键字名称和类型的列表元组。

返回
G:网络X图形

与线相对应的图形

实例

没有数据的边缘列表:

>>> lines = ["1 2", "2 3", "3 4"]
>>> G = nx.parse_edgelist(lines, nodetype=int)
>>> list(G)
[1, 2, 3, 4]
>>> list(G.edges())
[(1, 2), (2, 3), (3, 4)]

用python字典表示的数据的edgelist:

>>> lines = ["1 2 {'weight': 3}", "2 3 {'weight': 27}", "3 4 {'weight': 3.0}"]
>>> G = nx.parse_edgelist(lines, nodetype=int)
>>> list(G)
[1, 2, 3, 4]
>>> list(G.edges(data=True))
[(1, 2, {'weight': 3}), (2, 3, {'weight': 27}), (3, 4, {'weight': 3.0})]

数据在列表中的EdgeList:

>>> lines = ["1 2 3", "2 3 27", "3 4 3.0"]
>>> G = nx.parse_edgelist(lines, nodetype=int, data=(("weight", float),))
>>> list(G)
[1, 2, 3, 4]
>>> list(G.edges(data=True))
[(1, 2, {'weight': 3.0}), (2, 3, {'weight': 27.0}), (3, 4, {'weight': 3.0})]