Copy exemption Networkx
According to the doc , it seems that the method networkx.copy
is doing a deep copy of the graph. What worries me most is the statement
This makes a complete copy of the plot, including any node or edge attributes.
Does that mean it makes a copy of what the nodes contain? For example, if I have the following
class NodeContainer(object):
def __init__(self, stuff):
self.stuff = stuff
# ..other class stuff
g = networkx.DiGraph():
n1 = NodeContainer(stuff1)
n2 = NodeContainer(stuff2)
g.add_edge(n1,n2)
g2 = g.copy()
In line g2 = g.copy()
, does it make deep copies of objects NodeContainer
? If so, is there an existing shallow copy implementation? I couldn't find it. I am asking because I currently have a use to create a copy of the graph which I will edit (delete the nodes) but will not change the actual nodes themselves. So I don't need a deep copy in that sense, just a representation of the graph structure.
EDIT: If possible, I would also like to do a shallowreverse()
source to share
You can make a shallow copy using the class constructor. For example. for graphs,
In [1]: import networkx as nx
In [2]: G = nx.Graph()
In [3]: G.add_edge(1,2,l=['a','b','c'])
In [4]: H = nx.Graph(G) # shallow copy
In [5]: H[1][2]['l']
Out[5]: ['a', 'b', 'c']
In [6]: H[1][2]['l'].append('d')
In [7]: H[1][2]['l']
Out[7]: ['a', 'b', 'c', 'd']
In [8]: G[1][2]['l']
Out[8]: ['a', 'b', 'c', 'd']
source to share