Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
このコードを実行すると:
a = [1,2,3] b = a b.remove(2) print(a,b)
私が期待しているのは次のとおりです。
[1,2,3] [1,3]
しかし、これは私が本当に得るものです:
[1,3] [1,3]
呼び出しb.remove(2)も影響するのはなぜaですか? bに元のコンテンツのコピーを保持したまま、変更したい場合はどうすればよいaですか?
b.remove(2)
a
b
aとbは同じリストの 2 つの名前であるため、一方の名前でリストを変更すると、もう一方の名前で変更を確認できます。それらを別のリストにしたい場合は、コピーを作成します。
b = a[:]
また
b = list(a)