【NetworkX】Graph Optimization with NetworkX in Python
NetworkX 图优化算例:『Intro to Graph Optimization with NetworkX in Python - DataCamp』
使用 networkx 版本为 2.4 版。
【1】g.node[nlrow['id']] = nlrow[1:].to_dict()
报错 AttributeError: 'Graph' object has no attribute 'node'
新版改为 g.nodes[nlrow['id']].update(nlrow[1:].to_dict())
【2】 g.edges(data=True)[0:5]
报错 TypeError: 'EdgeDataView' object is not subscriptable
新版改为 list(g.edges(data=True))[0:5]
同理, g.nodes(data=True)[0:10] 改为 list(g.nodes(data=True))[0:10]
【3】 edge_colors = [e[2]['color'] for e in g.edges(data=True)]
报错 KeyError: 'color'
尝试查看 [e[2] for e in g.edges(data=True)] ,输出为:
[{'attr_dict': {'trail': 'rs', 'distance': 0.3, 'color': 'red', 'estimate': 0}}, {'attr_dict': {'trail': 'rs', 'distance': 0.21, 'color': 'red', 'estimate': 0}}, ... ]
因此改为 [e[2]['attr_dict']['color'] for e in g.edges(data=True)]
【4】 nodes_odd_degree = [v for v, d in g.degree_iter() if d % 2 == 1]
报错 AttributeError: 'Graph' object has no attribute 'degree_iter'
新版改为 nodes_odd_degree = [v for v, d in g.degree() if d % 2 == 1]
【5】 odd_matching = list(pd.unique([tuple(sorted([k, v])) for k, v in odd_matching_dupes.items()]))
报错 AttributeError: 'set' object has no attribute 'items'
改为 odd_matching = list(pd.unique([tuple(sorted([k, v])) for k, v in odd_matching_dupes]))