程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Misunderstanding about append to append array in Python

編輯:Python

Because it is not a computer related major , Therefore, it is impossible to use professional language to describe . Look at the code directly .

If you don't know this mistake , Then you can only debugger When I found out


# 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]]
# Failure to create a new array resulted in an error
# solve
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]]
# however
a = 1
ll =[]
ll.append(a)
ll.append(a)
print(ll)
a = 2
print(ll)
# output
# [1, 1]
# [1, 1]
# however
a1 = [1]
ll1 =[]
ll1.append(a1)
ll1.append(a1)
print(ll1)
a1 = [2]
print(ll1)
# output
#[[1], [1]]
#[[1], [1]]


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved