Aar*_*oke 2 python networkx python-3.x
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()))
Run Code Online (Sandbox Code Playgroud)
得到我以下错误:
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
Run Code Online (Sandbox Code Playgroud)
除了娜达,我还读了其他几篇文章。从我的简单理解来看,错误是由错误引起的,[0:5]但我很可能是错误的。
我是一个相当基础的编码人员,正在尝试按照本教程进行操作,但出现了以上错误。
该教程基于以前的版本,networkx该版本在哪里g.edges()或g.edges(Data=True)将为您提供元组列表。列表是可下标的。
您正在运行的版本具有不同的输出,g.edges为您提供了一个EdgeView属性和g.edges(data=True)一个EdgeDataView对象,这些对象无法下标。要回答您的问题,您可以执行以下操作:
list(g.edges(data=True))[0:5]
Run Code Online (Sandbox Code Playgroud)
注意::g.nodes()以前是一样的,list现在是NodeView不可下标的属性。因此,list在尝试添加下标([x:x])之前,请不要忘记将其转换为对象。
| 归档时间: |
|
| 查看次数: |
1294 次 |
| 最近记录: |