所以我试图从生成器中获取对象并将它们添加到列表中。但是当我将它们添加到列表中时,列表中填充了空对象而不是我添加的对象。这是为什么?
这段代码
#!/usr/bin/python
# Create a graph with 5 nodes
from snap import *
G = TUNGraph.New()
for i in range(5):
G.AddNode(i)
# Ids of these nodes are 0, 1, 2, 3, 4
for node in G.Nodes(): # G.Nodes() is a generator
print node.GetId()
lst = []
for node in G.Nodes():
lst.append(node)
print
# All of the nodes magically change to have ID -1
print [node.GetId() for node in lst]
print
# The nodes in the original graph are unchanged
for i in G.Nodes():
print i.GetId()
产生输出
0
1
2
3
4
[-1, -1, -1, -1, -1]
0
1
2
3
4