这是一个错误吗?由于名称相似,列表会覆盖另一个列表吗?

时间:2014-08-16 12:23:40

标签: python

我真的很困惑这个,似乎第二个列表覆盖了第一个列表,当它们之间绝对没有连接时 - 它们是2个不同的列表。如果不是错误,有人可以解释一下为什么?谢谢。

编辑:当runeWay_add列表是2个不同的列表时,它会覆盖runeWay_sub列表。最后,我只使用runeWay_sub重建程序,它就像它应该的那样。

>>> rune_cynumbers
[[53, 23, 67, 7, 17, 71, 31, 97, 2], [15, 8, 18, 3, 6, 19, 10, 24, 0], [16, 9, 19, 4, 7, 20, 11, 25, 1], [53, 23, 41, 67, 17, 71, 31, 107, 2], [15, 8, 12, 18, 6, 19, 10, 27, 0], [16, 9, 13, 19, 7, 20, 11, 28, 1]]
>>> rune_keynumbers
[[59, 23, 67, 31, 29, 53, 59, 97, 11], [16, 8, 18, 10, 9, 15, 16, 24, 4], [17, 9, 19, 11, 10, 16, 17, 25, 5], [5, 59, 67, 31, 29, 53, 59, 97, 11], [2, 16, 18, 10, 9, 15, 16, 24, 4], [3, 17, 19, 11, 10, 16, 17, 25, 5]]
>>> runeWay_sub = []
>>> runeWay_add = []
>>> for SET in rune_keynumbers:
...   micro_array = []
...   for i in SET:
...     micro_array.append(0)
...   runeWay_add.append(micro_array)
...   runeWay_sub.append(micro_array)
... 
>>> runeWay_add
[[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]]
>>> runeWay_sub
[[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]]
>>> for SET in enumerate(rune_keynumbers):
...   for i in enumerate(SET[1]):
...     runeWay_sub[SET[0]][i[0]] = rune_cynumbers[SET[0]][i[0]] - rune_keynumbers[SET[0]][i[0]]
...     runeWay_add[SET[0]][i[0]] = rune_cynumbers[SET[0]][i[0]] + rune_keynumbers[SET[0]][i[0]]
... 
>>> runeWay_sub
[[112, 46, 134, 38, 46, 124, 90, 194, 13], [31, 16, 36, 13, 15, 34, 26, 48, 4], [33, 18, 38, 15, 17, 36, 28, 50, 6], [58, 82, 108, 98, 46, 124, 90, 204, 13], [17, 24, 30, 28, 15, 34, 26, 51, 4], [19, 26, 32, 30, 17, 36, 28, 53, 6]]
>>> runeWay_add
[[112, 46, 134, 38, 46, 124, 90, 194, 13], [31, 16, 36, 13, 15, 34, 26, 48, 4], [33, 18, 38, 15, 17, 36, 28, 50, 6], [58, 82, 108, 98, 46, 124, 90, 204, 13], [17, 24, 30, 28, 15, 34, 26, 51, 4], [19, 26, 32, 30, 17, 36, 28, 53, 6]]
>>> import sys; print sys.version
2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2]
>>> runeWay_sub = []
>>> for SET in rune_keynumbers:
...   micro_array = []
...   for i in SET:
...     micro_array.append(0)
...   runeWay_sub.append(micro_array)
...
>>> runeWay_sub
[[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]]
>>> for SET in enumerate(rune_keynumbers):
...   for i in enumerate(SET[1]):
...     runeWay_sub[SET[0]][i[0]] = rune_cynumbers[SET[0]][i[0]] - rune_keynumbers[SET[0]][i[0]]
... 
>>> runeWay_sub
[[-6, 0, 0, -24, -12, 18, -28, 0, -9], [-1, 0, 0, -7, -3, 4, -6, 0, -4], [-1, 0, 0, -7, -3, 4, -6, 0, -4], [48, -36, -26, 36, -12, 18, -28, 10, -9], [13, -8, -6, 8, -3, 4, -6, 3, -4], [13, -8, -6, 8, -3, 4, -6, 3, -4]]

2 个答案:

答案 0 :(得分:2)

这两行:

runeWay_add.append(micro_array)
runeWay_sub.append(micro_array)

runeWay_addruneWay_sub是单独的列表,但您不是将micro_array的内容复制到每个列表中;您正在存储每个列表的引用。因此,当您执行

runeWay_sub[SET[0]][i[0]] = rune_cynumbers[SET[0]][i[0]] - rune_keynumbers[SET[0]][i[0]]
runeWay_add[SET[0]][i[0]] = rune_cynumbers[SET[0]][i[0]] + rune_keynumbers[SET[0]][i[0]]

表达式runeWay_sub[SET[0]]runeWay_add[SET[0]]都指向相同的列表。

这是一个更简单的例子,演示了完全相同的问题:

>>> c = [1,2,3]
>>> a = []
>>> b = []
>>> a.append(c)
>>> b.append(c)
>>> a[0][0] = 4
>>> a, b, c
([[4, 2, 3]], [[4, 2, 3]], [4, 2, 3])

ca[0]b[0]都引用完全相同的列表对象,因此通过{可以看到a[0][0]的分配{1}},ab

答案 1 :(得分:0)

之后:

>>> for SET in rune_keynumbers:
...   micro_array = []
...   for i in SET:
...     micro_array.append(0)
...   runeWay_add.append(micro_array)
...   runeWay_sub.append(micro_array)

runeWay_addruneWay_sub包含相同的可变列表元素,因此您对一个列表的元素所做的任何更改都将显示在另一个列表中。您可能希望将micro_array副本附加到runeWay_sub,而不是:

runeWay_sub.append(micro_array[:])