确认节点字典是否为空或是否包含指定属性。可以使用以下示例代码:
import networkx as nx
# 创建有向图
G = nx.DiGraph()
G.add_edges_from([(1,2),(2,3),(3,1)])
# 添加节点属性
nx.set_node_attributes(G, {1: {'color': 'red'}, 2: {'color': 'blue'}, 3: {'color': 'green'}})
# 获取节点属性
node_attr = nx.get_node_attributes(G, 'color')
if not node_attr:
print("节点属性为空")
else:
print(node_attr)
输出结果应为:
{1: 'red', 2: 'blue', 3: 'green'}
如果节点属性不为空,则返回一个字典,包含所有节点的指定属性。如果节点属性为空,则说明节点字典为空或没有指定属性。