import itertools
import copy
import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt
#--
edgelist = pd.read_csv('https://gist.githubusercontent.com/brooksandrew /e570c38bcc72a8d102422f2af836513b/raw/89c76b2563dbc0e88384719a35cba0dfc04cd522/edgelist_sleeping_giant.csv')
edgelist.head(10)
#--
nodelist = pd.read_csv('https://gist.githubusercontent.com/brooksandrew/f989e10af17fb4c85b11409fea47895b/raw/a3a8da0fa5b094f1ca9d82e1642b384889ae16e8/nodelist_sleeping_giant.csv')
nodelist.head(5)
#--
g = nx.Graph()
#--
for i, elrow in edgelist.iterrows():
g.add_edge(elrow[0], elrow[1], attr_dict=elrow[2:].to_dict())
#--
#print(elrow[0])
#print(elrow[1])
#print(elrow[2:].to_dict())
#--
g.edges(data=True)[0:5]
g.nodes(data=True)[0:10]
#--
print(format(g.number_of_edges()))
print(format(g.number_of_nodes()))
出现以下错误:
Traceback (most recent call last):
File "C:/Users/####/Main.py", line 22, in <module>
g.edges(data=True)[0:5]
TypeError: 'EdgeDataView' object is not subscriptable
除了nada,我还阅读了其他几个主题。
根据我的简单理解,错误是由 [0:5]
引起的,但我很可能是错的。
我是一个相当基础的编码员,正在努力遵循 this tutorial我得到上面的错误。
最佳答案
本教程基于 networkx
的先前版本,其中 g.edges()
或 g.edges(Data=True)
将给你一个元组列表。列表是可订阅的。
你运行的版本有不同的输出,g.edges
给你一个 EdgeView
属性,而 g.edges(data=True)
一个不可订阅的 EdgeDataView
对象。要回答您的问题,您可以:
list(g.edges(data=True))[0:5]
注意:对于 g.nodes()
也是如此:之前它是一个 list
现在它是一个不可订阅的 NodeView
属性。因此,在尝试添加下标 ([x:x]
) 之前,不要忘记将其转换为 list
对象。
关于python - 对象不可订阅 networkx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47325667/