networkx 之node edge

1:node

>>> import networkx as nx
>>> G=nx.Graph()
>>> G.add_node(1)
>>> G.add_nodes_form([2,5])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Graph' object has no attribute 'add_nodes_form'
>>> G.add_nodes_from([2, 3])
>>> G.add_nodes_from([4, 5])
>>> G.add_nodes_from([6, 8])
>>> G.node
NodeView((1, 2, 3, 4, 5, 6, 8))
>>> G.clear()
>>> G.node
NodeView(())
>>> H=nx.path_graph(10)
>>> G.add_nodes_from(H)
>>> G.node
NodeView((0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
>>> G.clear()
>>> G.node
NodeView(())

2;

import networkx as nx
import matplotlib.pyplot as plt
FG = nx.Graph()
FG.add_weighted_edges_from([(1, 2, 0.125), (1, 3, 0.75), (2, 4, 1.2), (3, 4, 0.375)])
for n, nbrs in FG.adj.items():
   for nbr, eattr in nbrs.items():
       wt = eattr['weight']
       if wt < 0.5: print('(%d, %d, %.3f)' % (n, nbr, wt))
for n,nbrs in FG.adj.items():
    for nbr,eattr in nbrs.items():
        wt=eattr['weight']
        if wt<0.5: 
               print('(%d ,%d ,%.3f)' % (n,nbr,wt))

猜你喜欢

转载自blog.csdn.net/weixin_42528089/article/details/91351593