我希望能够让许多节点具有相同的标签——在我的特定情况下,每个节点代表一篇新闻文章,并且应该用它们的新闻类别来标记它们。最终,我真正想要的是带有这些标签的 GML 文件。
这是一个小样本:
Gtest = nx.Graph()
nodes = [0, 1, 2, 3, 4]
labels = {0:"business", 1:"business",2:"sports", 3:"sports", 4:"politics"}
for node in nodes:
Gtest.add_node(node)
print Gtest.nodes(data=True)
"""
this prints:
[(0, {}), (1, {}), (2, {}), (3, {}), (4, {})]
Which is good, I want 5 nodes.
"""
Gtest = nx.relabel_nodes(Gtest, labels)
print Gtest.nodes(data=True)
"""this prints:
[('business', {}), ('politics', {}), ('sports', {})]
There are only 3 nodes.
"""
nx.write_gml(Gtest, "gml/stackoverflow_test", stringizer = None)
"""
This writes the GML file:
graph [
name "()"
node [
id 0
label "business"
]
node [
id 1
label "politics"
]
node [
id 2
label "sports"
]
]
"""
最终,我试图以 GML 文件结尾:
graph [
name "()"
node [
id 0
label "business"
]
node [
id 1
label "business"
]
node [
id 2
label "sports"
]
node [
id 3
label "sports"
]
node [
id 4
label "politics"
]
]
是否可以为多个节点使用相同的标签/生成此输出文件?