欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

networkx

程序员文章站 2024-03-15 21:26:06
...
  • 构造图
G = nx.DiGraph()

G.add_node("a", size = 12)
G.add_node("b", size = 20)
G.add_node("c", size = 50)

G.add_edge("a","b", weight=1)
G.add_edge("b","a", weight=5)
G.add_edge("a","c", weight=6)
G.add_edge("b","c", weight=5)
  • 节点的操作
for each in G.nodes(data=True):
    print each

('a', {'size': 12})
('c', {'size': 50})
('b', {'size': 20})

for each in G.nodes(data=True):
    print each[0]

a
c
b

for each in G.nodes(data=True):
    print each[1]['size']

12
50
20
  • 边的操作
for each in G.edges(data=True):
    print each

('a', 'c', {'weight': 6})
('a', 'b', {'weight': 1})
('b', 'a', {'weight': 5})
('b', 'c', {'weight': 5})

for each in G.edges(data=True):
    print each[2]['weight']

6
1
5
5
相关标签: networkx