我正在尝试在列表中添加数据。我使用了一个临时列表,将其数据交换到另一个列表b,然后在每次迭代中清除其数据 使用temp.clear()时,最终输出为空。但是使用temp = []时,输出正确。
请告诉我们为什么使用temp.clear()和temp = []时会有不同的输出。
a=['apple','pizza','veg','chicken','cheese','salad','chips','veg']
b=[]
temp=[]
for i in range(len(a)):
temp.append(a[i])
b.append(temp)
temp.clear()
#temp = []
print(b)
输出
#temp.clear()
[[],[],[]]
#temp = []
[['apple'],['pizza'],['veg'],['chicken'],['cheese'],['salad'],['chips'],['veg']]