Python:networkx 样例
2025/12/23大约 3 分钟
以graphml文件加载到networkx为例,文件基本信息:
<key id="d5" for="edge" attr.name="keywords" attr.type="string"/>
<key id="d4" for="edge" attr.name="description" attr.type="string"/>
<key id="d3" for="edge" attr.name="weight" attr.type="double"/>
<key id="d2" for="node" attr.name="source_id" attr.type="string"/>
<key id="d1" for="node" attr.name="description" attr.type="string"/>
<key id="d0" for="node" attr.name="entity_type" attr.type="string"/>
<graph edgedefault="directed">属性查看
def inspect_graphml_graph(G):
"""检查GraphML图的属性"""
print("=== 图基本信息 ===")
print(f"节点数: {G.number_of_nodes()}")
print(f"边数: {G.number_of_edges()}")
print("\n=== 节点属性示例 ===")
for node, data in list(G.nodes(data=True))[:3]: # 只看前3个
print(f"节点 {node}: {data}")
print("\n=== 边属性示例 ===")
for u, v, data in list(G.edges(data=True))[:3]: # 只看前3条
print(f"边 {u} -> {v}: {data}")
# 使用
inspect_graphml_graph(G)节点属性
# 获取节点及属性
nodes = list(G.nodes(data=True))
# 查看第一个节点
if nodes:
node_id, node_data = nodes[0]
print(f"节点ID: {node_id}")
print(f"节点属性: {node_data}")
# 访问具体属性
if 'entity_type' in node_data:
print(f"entity_type: {node_data['entity_type']}")
if 'description' in node_data:
print(f"description: {node_data['description']}")
if 'source_id' in node_data:
print(f"source_id: {node_data['source_id']}")
# 查看所有节点的属性名
all_node_attrs = set()
for node, data in G.nodes(data=True):
all_node_attrs.update(data.keys())
print(f"所有节点的属性名: {all_node_attrs}")边属性
访问边的标准方法
# 方法1:通过edges(data=True)
for u, v, data in G.edges(data=True):
print(f"边: {u} -> {v}, 属性: {data}")
# 方法2:通过get_edge_data
data = G.get_edge_data(u, v)
print(f"边属性: {data}")
# 方法3:通过边索引
data = G[u][v] # 或 G[u][v]['属性名']| 方法 | 返回内容 | 用途 |
|---|---|---|
G.get_edge_data(u, v) | 单条边的属性字典 | 查询特定边的属性 |
G.edges(node) | 与节点相关的多条边 | 获取节点的所有临边 |
G.neighbors(node) | 邻居节点列表 | 获取一跳邻居节点 |
G.edges() | 所有边 | 遍历图中的所有边 |
示例
els = [e for e in rag.graph.edges(data=True)]输出示例
els[0]=
('"背影"', '"我的儿子"', {'weight': 8.0, 'description': '"The image of the son’s back is recalled vividly by the narrator, evoking deep sorrow and remembrance."', 'keywords': '"memory, emotional connection"', 'source_id': 'chunk-6d907cf4fb3ba79ebc0719540b8b2971'})具体边
u, v, data = els[0] # data是包含所有属性的字典
print(f"边: {u} -> {v}")
print(f"所有属性: {data}")
# 访问具体属性
if 'keywords' in data:
print(f"keywords: {data['keywords']}")
if 'description' in data:
print(f"description: {data['description']}")
if 'weight' in data:
print(f"weight: {data['weight']}")查看边的属性名
u, v, data = els[0]
print(f"边的属性名: {list(data.keys())}")
# 查看所有边的所有可能属性名(去重)
all_edge_attrs = set()
for u, v, data in G.edges(data=True):
all_edge_attrs.update(data.keys())
print(f"所有边的属性名: {all_edge_attrs}")属性实用处理
根据属性筛选边
# 查找包含特定关键字的边
keyword_edges = []
for u, v, data in G.edges(data=True):
if 'keywords' in data and 'python' in data['keywords'].lower():
keyword_edges.append((u, v, data))
# 查找权重大于阈值的边
threshold = 0.5
high_weight_edges = [
(u, v, data) for u, v, data in G.edges(data=True)
if 'weight' in data and data['weight'] > threshold
]批量修改属性
# 为没有权重的边添加默认权重
for u, v, data in G.edges(data=True):
if 'weight' not in data:
G[u][v]['weight'] = 1.0检查特定边是否存在某个属性
# 安全访问方式
edge_attrs = G.get_edge_data(u, v)
if edge_attrs and 'keywords' in edge_attrs:
keywords = edge_attrs['keywords']
print(f"关键字: {keywords}")更新日志
2026/1/30 19:51
查看所有更新日志
72fda-于25785-于95b1c-于60a18-于
