这个问题在这里已有答案:
例如:
a = {};
b = [];
for i in range(20):
b.append(i)
a[i] = b
在循环之后,a的所有键都链接到相同的最终列表b [0,1,2,...,19]
。但是,我想要的是a[0] = [0]
,a[1] = [0,1]
,a[2] = [0,1,2]
。我不知道这里有什么问题。
a = {};
b = [];
for i in range(20):
b=[k for k in range(i)]
a[i] = b
希望能帮助到你