Last active
November 30, 2022 12:37
-
-
Save itrobotics/82241817696e72daeb3f8ed9030b99ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c = [[]]*3 #[[], [], []] | |
print(c) | |
c[0].append(100) | |
print(c) | |
print(id(c[0])) #c[0]==c[1]==c[2] | |
print(id(c[1])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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