对象不可订阅 [英] Object is not subscripable networkx

查看:84
本文介绍了对象不可订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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]引起的,但很可能是错误的.

I have read a couple of other threads but nada. From my simple understanding the error is caused by [0:5] but im most likely wrong.

我是位基本的编码人员,正在尝试遵循此教程,我得到上面的错误.

I'm a fairly basic coder and am trying to follow this tutorial and i get the error above.

推荐答案

本教程基于networkx的早期版本,其中g.edges()g.edges(Data=True)将为您提供元组列表.列表可以下标.

The tutorial is based on a previous version of networkx where g.edges() or g.edges(Data=True) would give you a list of tuples. Lists are subscriptable.

您正在运行的版本具有不同的输出,g.edges为您提供EdgeView属性,而g.edges(data=True)EdgeDataView对象,这些对象不能下标.要回答您的问题,您可以执行以下操作:

The version you are running has a different output, g.edges gives you an EdgeView property, and g.edges(data=True) an EdgeDataView object which are not subscriptable. To answer your question, you can do:

list(g.edges(data=True))[0:5]

注意:g.nodes()也是这样:在成为list之前,现在是NodeView不可下标的属性.因此,在尝试添加下标([x:x])之前,请不要忘记将其转换为list对象.

Note: the same is true for g.nodes(): before it was a list now it's a NodeView property not subscriptable. So don't forget to transform it to a list object before trying to add subscripts ([x:x]).

这篇关于对象不可订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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