2017-10-31 6 views
0

https://www.datacamp.com/community/tutorials/networkx-python-graph-tutorialはTypeError:アイテムの割り当てをサポートしていません 'NodeView' オブジェクト - 私はこのチュートリアルを働いていNetworkX

import itertools 
import copy 
import networkx as nx 
import pandas as pd 
import matplotlib.pyplot as plt  
nodelist = pd.read_csv('https://gist.githubusercontent.com/brooksandrew/f989e10af17fb4c85b11409fea47895b/raw/a3a8da0fa5b094f1ca9d82e1642b384889ae16e8/nodelist_sleeping_giant.csv') 

g = nx.Graph() 

for i, nlrow in nodelist.iterrows(): 
    g.node[nlrow['id']] = nlrow[1:].to_dict() 

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-80-35b1a259a02d> in <module>() 
     1 for i, nlrow in nodelist.iterrows(): 
----> 2  g.node[nlrow['id']] = nlrow[1:].to_dict() 

TypeError: 'NodeView' object does not support item assignment 

これを実行しているから、結果は次のようになります。

[('rs_end_south', {'X': 1865, 'Y': 1598}), 
('w_gy2', {'X': 2000, 'Y': 954}), 
('rd_end_south_dupe', {'X': 273, 'Y': 1869}), 
('w_gy1', {'X': 1184, 'Y': 1445}), 
('g_rt', {'X': 908, 'Y': 1378}), 
('v_rd', {'X': 258, 'Y': 1684}), 
('g_rs', {'X': 1676, 'Y': 775}), 
('rc_end_north', {'X': 867, 'Y': 618}), 
('v_end_east', {'X': 2131, 'Y': 921}), 
('rh_end_south', {'X': 721, 'Y': 1925})] 

しかし、私idの後ろにdictが出力されるようにPythonを得ることはできません。

+0

@マーメルありがとうございます。しかし、エラーの任意のアイデア? – jchaykow

+0

使用しているPythonのバージョンはどれですか? Python2とPython3の問題 – Murmel

+0

@Murmel python3 – jchaykow

答えて

2

の代わりに:

g.node[nlrow['id']] = nlrow[1:].to_dict() 

使用:

g.node[nlrow['id']].update(nlrow[1:].to_dict()) 

g.node[x]が辞書に他ならないので、これは動作します。それにもかかわらず、なぜドキュメンテーションが他の方法を提案するのか分かりません。

注:

Note - you're using networkx version 2.0, right? It's very recent, and so I suspect that this is an incompatibility from the person writing it using version 1.11. I think networkx provides ways to do what these commands are trying to do without directly editing the underlying data structure of the graph.

だから私のソリューションは、基本的には基礎となるデータ構造についての知識を有し、かつ使用しないことによって動作します。

はジョエルは、私は非常に重要であると考えている、良い点in the commentsを作りました公開api、これは良いプログラミングスタイルではありません。

関連する問題