关于[[a]] * b的坑
首先,我们都知道,python里可以这样生成一个列表:
>>> [ 0 ] * 5
[0, 0, 0, 0, 0]
>>>
有一天,我写出了这样一句代码出来:
>>> [[0]] * 5
[[0], [0], [0], [0], [0]]
>>>
看起来没问题?但请注意,they all point to the same object.
也就是说,这种生成方式对于整数之类的常量很cool,但对于list之类的东西却很不友好,可能会发生如下的情况:
>>> a = [[0]] * 5
>>> a
[[0], [0], [0], [0], [0]]
>>> a[0][0]=1
>>> a
[[1], [1], [1], [1], [1]]
>>>
官方文档是这样说的:
Note also that the copies are shallow; nested structures are not copied. This often haunts new Python programmers; consider:
>>> lists = [[]] * 3
>>> lists
[[], [], []]
>>> lists[0].append(3)
>>> lists
[[3], [3], [3]]
What has happened is that [[]] is a one-element list containing an empty list, so all three elements of [[]] * 3 are (pointers to) this single empty list. Modifying any of the elements of lists modifies this single list. You can create a list of different lists this way:
>>> lists = [[] for i in range(3)]
>>> lists[0].append(3)
>>> lists[1].append(5)
>>> lists[2].append(7)
>>> lists
[[3], [5], [7]]
也就是说matrix = [array] * 3操作中,只是创建3个指向array的引用,所以一旦array改变,matrix中3个list也会随之改变。
那么,如何正确生成呢?用列表生成式:
>>> b = [[0] for i in range(5)]
>>> b
[[0], [0], [0], [0], [0]]
>>> b[0][0] = 1
>>> b
[[1], [0], [0], [0], [0]]
>>>
看起来挺简单的坑,但我还是踩了。。。惭愧,所以记录下来