Skip to content

Instantly share code, notes, and snippets.

@itrobotics
Last active November 30, 2022 12:37
Show Gist options
  • Save itrobotics/82241817696e72daeb3f8ed9030b99ab to your computer and use it in GitHub Desktop.
Save itrobotics/82241817696e72daeb3f8ed9030b99ab to your computer and use it in GitHub Desktop.
c = [[]]*3 #[[], [], []]
print(c)
c[0].append(100)
print(c)
print(id(c[0])) #c[0]==c[1]==c[2]
print(id(c[1]))
d = [list() for i in range(3)] #[[], [], []] but each element is different
print(d)
d[0].append(100)
print(d)
print(id(d[0]))
print(id(d[1]))
#A container has a space to store references that can be used to refer to other objects.
a=[1,2,3]
mylist=[]
mylist.append(a)
a=[4,5,6]
mylist.append(a)
print('mylist1-->',mylist) #兩次append的a, refenece 到不同object id,
a=[4,5,6]
mylist2=[]
for i in range(3):
a[i]+=1 #[4,5,6]-->[5,6,7]
mylist2.append(a)
print('mylist2-->',mylist2)
mylist3=[]
for i in range(3):
b=[4,5,6] # reset b , so b is always a new object
b[i]+=1
#print(id(b))
mylist3.append(b)
print('mylist3-->',mylist3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment