Object is not subscripable networkx

2024/10/11 22:19:17
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()))

Gets me the following error:

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

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.

Answer

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.

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]

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]).

https://en.xdnf.cn/q/69721.html

Related Q&A

WTForms : How to add autofocus attribute to a StringField

I am rather new to WTForms, Flask-WTF. I cant figure out how to simply add the HTML5 attribute "autofocus" to one of the form field, from the form definition. I would like to do that in the P…

Image rotation in Pillow

I have an image and I want to transpose it by 30 degrees. Is it possible to do by using something like the following?spinPicture003 = Picture003.transpose(Image.Rotate_30)

Python code to calculate angle between three points (lat long coordinates)

Can anybody suggest how to calculate angle between three points (lat long coordinates)A : (12.92473, 77.6183) B : (12.92512, 77.61923) C : (12.92541, 77.61985)

z3: solve the Eight Queens puzzle

Im using Z3 to solve the Eight Queens puzzle. I know that each queen can be represented by a single integer in this problem. But, when I represent a queen by two integers as following:from z3 import *X…

Image skewness kurtosis in python

Is there a python package that will provide me a way to clacluate Skewness and Kurtosis of an image?. Any example will be great.Thanks a lot.

Python: Getting all the items out of a `threading.local`

I have a threading.local object. When debugging, I want to get all the objects it contains for all threads, while I am only on one of those threads. How can I do that?

Why tuple is not mutable in Python? [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Why are python strings and tuples are made immutable? What lower-level design makes tuple not mutable in Python? Why th…

Display Django form fields on the same line

I would like to display two form fields on the same line and not each one after the other one.For the moment, I get:Choice a theme :. Datasystems. CamerounBut I would like to display this form like:Cho…

How can I use the index array in tensorflow?

If given a matrix a with shape (5,3) and index array b with shape (5,), we can easily get the corresponding vector c through,c = a[np.arange(5), b]However, I cannot do the same thing with tensorflow,a …

cython with array of pointers

I have a list of numpy.ndarrays (with different length) in python and need to have very fast access to those in python. I think an array of pointers would do the trick. I tried:float_type_t* list_of_ar…