Created
August 14, 2019 12:47
-
-
Save pn11/fc12cd366f9c75727939b34c4c4d2663 to your computer and use it in GitHub Desktop.
Creating two-dimensional array in Python. Result -> https://ideone.com/M2TpNg
This file contains 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
li = [[]] * 2 | |
print(li) | |
li[0].append(1) | |
print(li) | |
print(id(li[0])) | |
print(id(li[1])) | |
li2 = [[] for _ in range(2)] | |
print(li2) | |
li2[0].append(1) | |
print(li2) | |
print(id(li2[0])) | |
print(id(li2[1])) | |
# Result in Ideone | |
# https://ideone.com/M2TpNg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[[]]*2
のように2次元配列を作ると、[[],[]]
の中の2つの[]
が同じ実体になっていて、片方に append すると両方に要素が追加される。リスト内包表記でつくればこのようなことはない。