Networkx報錯 AttributeError: 'NodeView' object has no attribute 'remove'

錯誤描述

我用的版本是python2.7+networkx2.2。部分代碼:

#G2是傳入的一個隨機網絡
unsaturated_b = G2.nodes()
unsaturated_b.remove(node_b)

最後一行報錯,提示AttributeError: 'NodeView' object has no attribute 'remove'

解決辦法

這是由於networkx1.x和networkx2.x版本不同而導致的,因爲沒有使用列表而是NodeView。

可以轉換爲列表 list(G.nodes())

unsaturated_b = G2.nodes()
list(unsaturated_b).remove(node_b)

更多信息:

https://networkx.github.io/documentation/latest/release/migration_guide_from_1.x_to_2.0.html

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章