因為不是計算機相關專業,所以無法用專業語言描述。請直接看代碼。
這一誤區如果你不知道,那你只能在debugger 時發現了
# twopics
import random
ls1 = []
ls = [2,2]
for i in range(1,3):
ls[0] = random.randint(1,88)
ls[1] = random.randint(1,88)
ls1.append(ls)
print(ls1)
# output : [[55, 62], [55, 62]] , [[48, 16], [48, 16]] , [[41, 48], [41, 48]]
# 未創建新數組導致出錯
# 解決
lss1 = []
lss = [2,2]
for i in range(1,3):
l = []
lss[0] = random.randint(1,88)
lss[1] = random.randint(1,88)
l = [lss[0],lss[1]]
lss1.append(l)
print(lss1)
# output : [[14, 81], [45, 56]] . [[25, 69], [17, 14]]
# 但是
a = 1
ll =[]
ll.append(a)
ll.append(a)
print(ll)
a = 2
print(ll)
# output
# [1, 1]
# [1, 1]
# 但是
a1 = [1]
ll1 =[]
ll1.append(a1)
ll1.append(a1)
print(ll1)
a1 = [2]
print(ll1)
# output
#[[1], [1]]
#[[1], [1]]