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: NetworkX 图

与行对应的图

另请参阅

read_weighted_edgelist

示例

没有数据的边列表

>>> 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 dictionary representation 数据的边列表

>>> 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})]

包含列表数据的边列表

>>> 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})]