Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:初始化空嵌套列表时的奇怪列表行为_Python_Python 3.x_List - Fatal编程技术网

Python:初始化空嵌套列表时的奇怪列表行为

Python:初始化空嵌套列表时的奇怪列表行为,python,python-3.x,list,Python,Python 3.x,List,有人能解释一下原因吗: b = 10*[[]] b[0].append(3) b现在是: [[3], [3], [3], [3], [3], [3], [3], [3], [3], [3]] 但如果我这样做: b = [[] for _ in range(10)] b[0].append(3) b现在是: [[3], [], [], [], [], [], [], [], [], []] 还要注意 10*[[]] == [[] for [] in range(10)] 返回 True

有人能解释一下原因吗:

b = 10*[[]]
b[0].append(3)
b现在是:

[[3], [3], [3], [3], [3], [3], [3], [3], [3], [3]]
但如果我这样做:

b = [[] for _ in range(10)]
b[0].append(3)
b现在是:

[[3], [], [], [], [], [], [], [], [], []]
还要注意

10*[[]] == [[] for [] in range(10)]
返回

True
(在Python 3.6.1中测试)

 because:
    >>> b = 10*[[]]
    >>> b[0] is b[1]
    True
    >>> b = [[] for _ in range(10)]
    >>> b[0] is b[1]
    False
    >>>