MultiDiGraph.update#
- MultiDiGraph.update(edges=None, nodes=None)#
使用节点/边/图作为输入更新图。
类似于字典的
update
方法,该方法接受一个图作为输入,将该图的节点和边添加到当前图中。它也可以接受两个输入:边和节点。最后,它可以只接受边或节点。要仅指定节点,必须使用关键字nodes
。边和节点的集合的处理方式类似于
add_edges_from
/add_nodes_from
方法。当迭代时,它们应产生 2 元组 (u, v) 或 3 元组 (u, v, datadict)。- Parameters:
See also
add_edges_from
向图中添加多条边
add_nodes_from
向图中添加多个节点
Notes
如果你想使用邻接结构更新图,可以直接从邻接结构中获取边/节点。以下示例提供了常见情况,你的邻接结构可能略有不同,需要调整这些示例:
>>> # 集合/列表/元组的字典 >>> adj = {1: {2, 3}, 2: {1, 3}, 3: {1, 2}} >>> e = [(u, v) for u, nbrs in adj.items() for v in nbrs] >>> G.update(edges=e, nodes=adj)
>>> DG = nx.DiGraph() >>> # 属性的字典的字典 >>> adj = {1: {2: 1.3, 3: 0.7}, 2: {1: 1.4}, 3: {1: 0.7}} >>> e = [ ... (u, v, {"weight": d}) ... for u, nbrs in adj.items() ... for v, d in nbrs.items() ... ] >>> DG.update(edges=e, nodes=adj)
>>> # 字典的字典的字典 >>> adj = {1: {2: {"weight": 1.3}, 3: {"color": 0.7, "weight": 1.2}}} >>> e = [ ... (u, v, {"weight": d}) ... for u, nbrs in adj.items() ... for v, d in nbrs.items() ... ] >>> DG.update(edges=e, nodes=adj)
>>> # 前驱邻接(集合的字典) >>> pred = {1: {2, 3}, 2: {3}, 3: {3}} >>> e = [(v, u) for u, nbrs in pred.items() for v in nbrs]
>>> # 多图的字典的字典的字典的字典 >>> MDG = nx.MultiDiGraph() >>> adj = { ... 1: {2: {0: {"weight": 1.3}, 1: {"weight": 1.2}}}, ... 3: {2: {0: {"weight": 0.7}}}, ... } >>> e = [ ... (u, v, ekey, d) ... for u, nbrs in adj.items() ... for v, keydict in nbrs.items() ... for ekey, d in keydict.items() ... ] >>> MDG.update(edges=e)
Examples
>>> G = nx.path_graph(5) >>> G.update(nx.complete_graph(range(4, 10))) >>> from itertools import combinations >>> edges = ( ... (u, v, {"power": u * v}) ... for u, v in combinations(range(10, 20), 2) ... if u * v < 225 ... ) >>> nodes = [1000] # 对于单例,使用容器 >>> G.update(edges, nodes)