[0表示范围(10)][0表示范围(10)]和[0表示范围(10)]* 10之间的区别是什么?(复制)

时间:2021-08-18 18:26:59

This question already has an answer here:

这个问题已经有了答案:

>>> CM = [[0 for _ in range(10)]] * 10
>>> CM    
[[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, 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]]
>>> CM[0][0] = CM[0][0] + 1
>>> CM
[[1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

I was trying to create a confusion matrix. It basically contains count of the (i, j) pairs. I first created a list of lists, and then incremented the appropriate variable. However, it didn't work as expected. CM[i][0] got incremented for all values of i.

我试图创建一个混乱矩阵。它基本上包含(i, j)对的计数。我首先创建了一个列表列表,然后递增适当的变量。然而,它并没有像预期的那样工作。CM[i][0]对i的所有值都进行了递增。

I found a work around.

我在附近找到了一份工作。

>>> CM = [[0 for _ in range(10)] for _ in range(10)]
>>> CM
[[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, 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]]
>>> CM[0][0] = CM[0][0] + 1
>>> CM
[[1, 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, 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]]

But I would be grateful if someone could explain why the first method failed.

但是如果有人能解释第一个方法失败的原因,我会很感激。

1 个解决方案

#1


3  

>>> CM = [[0 for _ in range(10)]] * 10

Is copying a reference to the same object, ten times. It is equivalent to this:

复制对同一对象的引用,十次。它等价于:

>>> x = [0 for _ in range(10)]
>>> CM = [x, x, x, x, x, x, x, x, x, x]

So manipulating one element causes side effects. Your workaround is elegant and correct.

所以操作一个元素会产生副作用。你的工作环境优雅而正确。

>>> CM = [[0 for _ in range(10)]] * 10

Is copying a reference to the same object, ten times. It is equivalent to this:

复制对同一对象的引用,十次。它等价于:

>>> x = [[0 for _ in range(10)]]
>>> CM = [x, x, x, x, x, x, x, x, x, x]

So manipulating one element causes side effects. Your workaround is elegant and correct.

所以操作一个元素会产生副作用。你的工作环境优雅而正确。

Note:

注意:

This occurs since the elements of the lists are lists (which are mutable). If they were strings for example, which are immutable, it wouldn't be an issue if the same string was referenced in different lists, since they can't be manipulated. Python doesn't like to waste memory (unless explicitly told to ie. deepcopy), so copying lists will simply copy their references.

这是因为列表的元素是列表(它们是可变的)。如果它们是不可变的字符串,如果相同的字符串在不同的列表中被引用,那就不是问题了,因为它们不能被操纵。Python不喜欢浪费内存(除非明确告诉ie)。因此,复制列表只需要复制它们的引用。

#1


3  

>>> CM = [[0 for _ in range(10)]] * 10

Is copying a reference to the same object, ten times. It is equivalent to this:

复制对同一对象的引用,十次。它等价于:

>>> x = [0 for _ in range(10)]
>>> CM = [x, x, x, x, x, x, x, x, x, x]

So manipulating one element causes side effects. Your workaround is elegant and correct.

所以操作一个元素会产生副作用。你的工作环境优雅而正确。

>>> CM = [[0 for _ in range(10)]] * 10

Is copying a reference to the same object, ten times. It is equivalent to this:

复制对同一对象的引用,十次。它等价于:

>>> x = [[0 for _ in range(10)]]
>>> CM = [x, x, x, x, x, x, x, x, x, x]

So manipulating one element causes side effects. Your workaround is elegant and correct.

所以操作一个元素会产生副作用。你的工作环境优雅而正确。

Note:

注意:

This occurs since the elements of the lists are lists (which are mutable). If they were strings for example, which are immutable, it wouldn't be an issue if the same string was referenced in different lists, since they can't be manipulated. Python doesn't like to waste memory (unless explicitly told to ie. deepcopy), so copying lists will simply copy their references.

这是因为列表的元素是列表(它们是可变的)。如果它们是不可变的字符串,如果相同的字符串在不同的列表中被引用,那就不是问题了,因为它们不能被操纵。Python不喜欢浪费内存(除非明确告诉ie)。因此,复制列表只需要复制它们的引用。