I have two networks G
and G1
that are generated with networkx
.
In order to find a list containing unique nodes between the two networks (i.e. intersection), I run in Python 2.7
the following command which works perfectly.
tmp2 = list(set(G.nodes) & set(G1.nodes))
The same command in a machine with Python 3
returns the following error:
TyperError: 'method' object is not iterable
Is there a way to avoid this error in Python 3.x
?
In networkx version 1.x, G.nodes
is a method, and it returns the nodes of G
. G.nodes()
is a list of nodes of G
. So your call to set(G.nodes)
is asking Python to make a set out of a method. That can't be done. It can make a set out of the nodes of G
. So set(G.nodes())
works.
In version 2.x, G.nodes
and G.nodes()
both are NodeView
objects. These operate a lot like lists (though there are differences).
Thus your command will run in the most recent version of networkx, but not in older versions. Even if you switch to using 2.x
, I recommend still calling it as G.nodes()
in case the code is ever run in an earlier version.