问题描述
根据 doc ,看来方法会对图进行深层复制.我最担心的是声明
According the doc, it appears that the networkx.copy
method does a deep copy of the graph. I'm most concerned about the statement
这是否暗示它也复制了节点包含的内容?例如,如果我有以下内容
Is this suggesting that it makes a copy of what the nodes contain as well? 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()
在g2 = g.copy()
行中是否也在复制NodeContainer
对象的深层副本?如果是这样,是否存在浅表副本的现有实现?我一直找不到.我之所以这样问是因为我目前已用于创建一个图形副本,该图形我将进行编辑(从中删除节点),但不会更改实际节点本身.因此,从这个意义上讲,我不需要深度复制,而只是图形结构的表示.
In the g2 = g.copy()
line is it making deep copies of the NodeContainer
objects as well? If so, is there an existing implementation of a shallow copy? I have not been able to find one. I ask because I currently have use to create a copy a graph that I will edit (remove nodes from) but not change the actual nodes themselves. So I don't need a deep copy in that sense, just a representation of the graph structure.
编辑:如果可能的话,我也想做一个浅的reverse()
If possible I'd also like to do a shallow reverse()
推荐答案
您可以使用类构造函数进行浅表复制.例如.对于图形,
You can make a shallow copy by using the class constructor. E.g. 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']
这篇关于Networkx复制说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!