[英]Networkx set complex node attribute
我想單獨更新某個名為“旅程”的新自定義屬性的節點,但我遇到了嚴重的困難。
說我有一些圖表:
import pandas as pd
import networkx as nx
cols = ['node_a','node_b','travel_time','attribute']
data = [['A','B',3,'attribute1'],
['B','C',1,'attribute1'],
[ 'C','D',7,'attribute1'],
['D','E',3,'attribute1'],
['E','F',2,'attribute1'],
['F','G',4,'attribute1'],
['A','L',4,'attribute2'],
['L','D',3,'attribute2']
]
edges = pd.DataFrame(data)
edges.columns = cols
G=nx.convert_matrix.from_pandas_edgelist(edges,'node_a','node_b', ['travel_time','attribute'])
對於每個節點,我想以{direction: [[id,timestamp, set_of_carrying_items]]}
的形式添加一個屬性,其中內部是一個列表列表,因為我想添加更多[id,timestamp, carrying_items]
到它。
示例:使用以下內容更新特定節點 A
new_attribute = {'A':{'up': [[0, Timestamp('1900-01-01 05:31:00'), set()]]}}}
但是,無論我嘗試什么,節點都無法正確更新。 nx.get_node_attributes(G, 'A')
返回一個空字典。 但是nx.get_node_attributes(G,'up')
返回屬性!!
似乎我設置錯誤,但我不知道如何。 有人知道正確的方法嗎?
使用 networkx 2.4
解決了。
nx.get_node_attributes(G,X)
為所有節點返回名為 X 的屬性。 您正在尋找G.node['nodeA']
如果您的屬性的形式為{'nodeA':{'attribute_name': value}}
,您將為節點nodeA
將value
設置為attribute_name
。
例如,就我而言:
att = {'nodeA':{'up': [[1, Timestamp('1900-01-01 22:31:00'), set()]]}}}
nx.set_node_attributes(G,att)
作品
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.