[networkx-discuss] AttributeError: 'generator' object has no attribute 'iteritems'

636 views
Skip to first unread message

Franck Kalala

unread,
Feb 13, 2022, 4:28:43 PM2/13/22
to networkx...@googlegroups.com
Hi Folk

I always use the below piece of code to compute the similarity matrix. This time I am getting an error. This is the piece of code:

import networkx as nx
import numpy, matplotlib
from scipy.cluster import hierarchy
from scipy.spatial import distance

G = nx.karate_club_graph()
n = len(G.nodes())
path_length = nx.all_pairs_shortest_path_length(G)
distances = numpy.zeros((n,n))

for u,p in path_length.iteritems():
    for v,d in p.iteritems():
        distances[int(u)-1][int(v)-1] = d 


and this the error message:

AttributeError                            Traceback (most recent call last)
/var/folders/31/w56d47t956zfp0kklgtz_yq40000gn/T/ipykernel_50535/4052642967.py in <module>
      9 distances = numpy.zeros((n,n))
     10 
---> 11 for u,p in path_length.iteritems():
     12     for v,d in p.iteritems():
     13         distances[int(u)-1][int(v)-1] = d

AttributeError: 'generator' object has no attribute 'iteritems'

I am having the same error message when replacing iteritems by items.

Is there a new way to access those items?

Best

F


Dan Schult

unread,
Feb 13, 2022, 5:19:12 PM2/13/22
to networkx...@googlegroups.com
Well...  it is not a new change.  But given that you are used to using `.iteritems`, it is newer than that but still many years ago.

The `nx.all_pairs_shortest_path_length(G)` function is a generator that yields the `u, p` values.
You could use it as:`   path_length = dict(nx.all_pairs_shortest_path_length(G))`
networkx.org has all the documentation now which makes it easy to find info on e.g. the shortest_path functions.

Reply all
Reply to author
Forward
0 new messages