我想追加列表B中的數據到列表A,以在列表內創建列表,使用listA.append(listB)不工作,我是新手到python有什麼額外需要添加?追加列表B到列表A和刪除列表B元素的新元素python
這裏是示例代碼
`from random import randint
def call():
c=0
a=[]
b=[]
while (c != 10):
b.append(randint(0,9))
print b
a.append(b)
print a
del b[:]
c=c+1
for i in range(2):
call()`
這裏輸出
`[7]
[[7]]
[3]
[[3], [3]]
[3]
[[3], [3], [3]]
[8]
[[8], [8], [8], [8]]
[0]
[[0], [0], [0], [0], [0]]
[7]
[[7], [7], [7], [7], [7], [7]]
[3]
[[3], [3], [3], [3], [3], [3], [3]]
[9]
[[9], [9], [9], [9], [9], [9], [9], [9]]
[2]
[[2], [2], [2], [2], [2], [2], [2], [2], [2]]
[1]
[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]
[8]
[[8]]
[0]
[[0], [0]]
[0]
[[0], [0], [0]]
[6]
[[6], [6], [6], [6]]
[2]
[[2], [2], [2], [2], [2]]
[7]
[[7], [7], [7], [7], [7], [7]]
[9]
[[9], [9], [9], [9], [9], [9], [9]]
[7]
[[7], [7], [7], [7], [7], [7], [7], [7]]
[6]
[[6], [6], [6], [6], [6], [6], [6], [6], [6]]
[1]
[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]`
我不明白爲什麼會出現這種情況?
什麼是期望的輸出?實際上,你的列表中有列表。 – mhoff
所需出放是''[7] [[7]] [3] [[3],[7]] [3] [[3],[3],[7]] [8] [[8],[3],[3],[7]] [0] [[0] [8] [3] [7]] [7] ] [[7],[0],[8],[3],[3],[7]] ' –
@GeminiKeith我想將listB的當前實例中的所有內容附加到listA。我曾試過'listA = listA + listB',但是這給了我''0,1]'類型的列表A –