【发布时间】:2018-07-06 21:53:01
【问题描述】:
请看下面的代码:
a=[1,2,3,4]
m=[0,0]
q=[]
for n in a:
m[0]=n
for n in a:
m[1]=n
q.append(m)
print(q)
所需的输出应该是:
[[1, 1], [1, 2], [1, 3], [1, 4], [2, 1], [2, 2], [2, 3], [2, 4], [3, 1], [3, 2], [3, 3], [3, 4], [4, 1], [4, 2], [4, 3], [4, 4]]
但输出却是:
[[4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4]]
有什么想法吗?
【问题讨论】:
-
您每次都附加相同的列表。
标签: python python-3.x list loops for-loop