>>>a=[[]]*3
>>>a
[[], [], []]
>>>a[0].append(1)
>>>a
[[1], [1], [1]]
原因是用上述方法定义的二维列表的每个子列表都来自同一块数据区。
修改:
>>>def create_2dim_list(t):
list_=[]
for i in range(0,t):
list_.append([])
return list_
>>>a=create_2dim_list(3)
>>>a
[[], [], []]
>>>a[0].append(1)
>>>a
[[1], [], []]