TypeError:"NodeView"对象不支持项目分配-NetworkX [英] TypeError: 'NodeView' object does not support item assignment - NetworkX

查看:171
本文介绍了TypeError:"NodeView"对象不支持项目分配-NetworkX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在完成本教程: https://www. datacamp.com/community/tutorials/networkx-python-graph-tutorial

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

运行此命令的结果应类似于:

The result from running this should look like:

[('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})]

但是我无法让python输出id后跟字典.

But I can't get python to output the id followed by the dict.

推荐答案

而不是:

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

使用:

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

之所以有用,是因为g.node[x]就是字典.不过,我不确定文档为什么提出另一种方式.

This works because g.node[x] is nothing else than a dict. Nevertheless, I'm not sure why the documentation proposes the other way.

注意:

Joel在评论中 /a>,我认为这很重要:

Joel made a good point in the comments, which I think is very important:

注意-您使用的是networkx 2.0版,对吗?这是最近的事, 所以我怀疑这与写作人不兼容 它使用1.11版.我认为networkx提供了执行这些操作的方法 命令试图在不直接编辑基础数据的情况下进行操作 图的结构.

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.

因此,我的解决方案基本上是通过了解底层数据结构并且不使用公共api 来工作的,这不是一种好的编程风格.

So my solution basically works by having knowledge about the underlying data structure and not using the public api, which is not good programming style.

这篇关于TypeError:"NodeView"对象不支持项目分配-NetworkX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆