我正面临着一个我现在无法自行解决的问题。 它关于以下片段:
counter = 0
appendList = []
valueList = [[0], [0]]
for i in range(0,3):
valueList[1] = counter
print "Loop " , i , " valueList: " , valueList
print "Appending (valueList): " , valueList , " to (appendList): " , appendList
appendList.append(valueList)
counter = counter + 1
print "Final appendList: " , appendList
这导致以下输出:
Loop 0 valueList: [[0], 0]
Appending (valueList): [[0], 0] to (appendList): []
Loop 1 valueList: [[0], 1]
Appending (valueList): [[0], 1] to (appendList): [[[0], 1]]
Loop 2 valueList: [[0], 2]
Appending (valueList): [[0], 2] to (appendList): [[[0], 2], [[0], 2]]
Final appendList: [[[0], 2], [[0], 2], [[0], 2]]
我希望代码段向List-Items
添加不同的appendList
。最终结果应如下所示:
[[[0], 0], [[0], 1], [[0], 2]]
但正如您所看到的,该代码段使用最高计数器的相同值填充appendList
。
有人可以向我解释一下这种行为,还是告诉我,我的错误在哪里?
答案 0 :(得分:2)
valueList
每次附加时都是相同的对象,因此在一个地方修改它似乎可以在任何地方修改它。
>>> a = [0]
>>> b = a
>>> a[0] = 42
>>> b
[42]
您需要附加一份副本,以便每次都添加新列表。
appendList.append(valueList[:])
答案 1 :(得分:1)
您可以尝试:
counter = 0
appendList = []
valueList = [[0], [0]]
for i in range(0,3):
valueList[1] = counter
print "Loop " , i , " valueList: " , valueList
print "Appending (valueList): " , valueList , " to (appendList): " , appendList
appendList.append(valueList[:])
counter = counter + 1
print "Final appendList: " , appendList
输出:
Loop 0 valueList: [[0], 0]
Appending (valueList): [[0], 0] to (appendList): []
Loop 1 valueList: [[0], 1]
Appending (valueList): [[0], 1] to (appendList): [[[0], 0]]
Loop 2 valueList: [[0], 2]
Appending (valueList): [[0], 2] to (appendList): [[[0], 0], [[0], 1]]
Final appendList: [[[0], 0], [[0], 1], [[0], 2]]
<强>解释强>
分配valuelist
对象时,所有元素都从referance对象分配。因此,b = a
代替b = a [:]
,因为它每次只能从此对象进行复制。
>>> a = [1, 2, 3]
>>> b = a
>>> a[:] = [4, 5, 6]
>>> b
[4, 5, 6]
>>> a
[4, 5, 6]
>>> b = a [:]
>>> a = [1, 2, 3]
>>> b
[4, 5, 6]
>>> a
[1, 2, 3]
>>>
答案 2 :(得分:0)
appendList
包含当前值valueList
的引用,因此当您修改valueList
时,appendList
中的副本会发生变化。每次放入valueList
时,您需要创建appendList
的副本。
答案 3 :(得分:0)
valueList仍指向单个对象。您可以通过打印id(valueList)
来检查这一点。 id提供了对象的标识。即对象的内存地址。在您的示例中,如果您打印id,它将始终相同。
counter = 0
appendList = []
valueList = [[0], [0]]
for i in range(0,3):
print "id of valueList before", id(valueList)
#creates a copy of valueList.
valueList = valueList[:]
print "id of valueList after", id(valueList)
valueList[1] = counter
print "Loop " , i , " valueList: " , valueList
print "Appending (valueList): " , valueList , " to (appendList): ", appendList
appendList.append(valueList)
counter = counter + 1
print "Final appendList: " , appendList
答案 4 :(得分:0)
counter = 0
appendList = []
valueList = [[0], [0]]
print(valueList[1][0])
for i in range(0,3):
print(counter)
valueList[1][0] = counter # this was wrong
print ("Loop " , i , " valueList: " , valueList)
print ("Appending (valueList): " , valueList , " to (appendList): " , appendList)
appendList.append(valueList)
valueList = [[0], [0]] # and this was absent
counter += 1
print(appendList)
[[[0], [0]], [[0], [1]], [[0], [2]]]
答案 5 :(得分:0)
一个好习惯也是检查代码的效率:你正在定义多个变量,而对我来说,你对appendList感兴趣。
appendList = [ [[0], x] for x in range(3) ] # doStuff for index in range of n
print appendList
使用列表理解,您可以将其简化为必要的计算。