本文简要介绍
networkx.Graph.update
的用法。用法:
Graph.update(edges=None, nodes=None)
使用节点/边/图形作为输入来更新图形。
与 dict.update 一样,此方法将图作为输入,将图的节点和边添加到该图中。它也可以有两个输入:边和节点。最后,它可以采用边或节点。要仅指定节点,必须使用关键字
nodes
。边和节点的集合的处理方式与add_edges_from/add_nodes_from 方法类似。迭代时,它们应该产生 2 元组 (u, v) 或 3 元组 (u, v, datadict)。
参数:
注意:
如果您想使用邻接结构更新图形,则可以直接从邻接中获取边/节点。以下示例提供了常见情况,您的邻接可能略有不同,需要对这些示例进行调整:
>>> # dict-of-set/list/tuple >>> 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() >>> # dict-of-dict-of-attribute >>> 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)
>>> # dict-of-dict-of-dict >>> 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)
>>> # predecessor adjacency (dict-of-set) >>> pred = {1: {2, 3}, 2: {3}, 3: {3}} >>> e = [(v, u) for u, nbrs in pred.items() for v in nbrs]
>>> # MultiGraph dict-of-dict-of-dict-of-attribute >>> 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)
例子:
>>> 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] # for singleton, use a container >>> G.update(edges, nodes)
相关用法
- Python NetworkX Graph.size用法及代码示例
- Python NetworkX Graph.to_undirected用法及代码示例
- Python NetworkX Graph.degree用法及代码示例
- Python NetworkX Graph.number_of_edges用法及代码示例
- Python NetworkX Graph.add_weighted_edges_from用法及代码示例
- Python NetworkX Graph.subgraph用法及代码示例
- Python NetworkX Graph.__contains__用法及代码示例
- Python NetworkX Graph.nodes用法及代码示例
- Python NetworkX Graph.number_of_nodes用法及代码示例
- Python NetworkX Graph.clear用法及代码示例
- Python NetworkX Graph.add_edge用法及代码示例
- Python NetworkX Graph.has_node用法及代码示例
- Python NetworkX Graph.add_node用法及代码示例
- Python NetworkX Graph.edges用法及代码示例
- Python NetworkX Graph.__len__用法及代码示例
- Python NetworkX Graph.remove_nodes_from用法及代码示例
- Python NetworkX Graph.remove_edge用法及代码示例
- Python NetworkX Graph.__init__用法及代码示例
- Python NetworkX Graph.remove_node用法及代码示例
- Python NetworkX Graph.edge_subgraph用法及代码示例
- Python NetworkX Graph.__iter__用法及代码示例
- Python NetworkX Graph.to_directed用法及代码示例
- Python NetworkX Graph.order用法及代码示例
- Python NetworkX Graph.add_edges_from用法及代码示例
- Python NetworkX Graph.copy用法及代码示例
注:本文由纯净天空筛选整理自networkx.org大神的英文原创作品 networkx.Graph.update。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。