本文簡要介紹
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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。