0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Python】deepcopy

Posted at

【Python】deepcopyについて

メモ程度に書いてみた。

deepcopy(深いコピー)は、例えば下記のように元を変更してもコピー先には、影響しない。

import copy

original_list = [[1, 3], [5, 4]]
copied_list = copy.deepcopy(original_list)

# コピー元を変更しても、コピー先には影響がない
original_list[1][1] = 100  # リストのインデント1番のリストのさらに1番目を100に
print(original_list)
print(copied_list)
[[1, 3], [5, 100]]  # original_list コピー元
[[1, 3], [5, 4]]    # copied_list コピー先

逆も同じことです。

import copy

original_list = [[1, 3], [5, 4]]
copied_list = copy.deepcopy(original_list)

# コピー先を変更しても、コピー元には影響がない
copied_list[1][1] = 100  # リストのインデント1番のリストのさらに1番目を100
print(original_list)
print(copied_list)
[[1, 3], [5, 4]]     # original_list コピー元
[[1, 3], [5, 100]]   # copied_list コピー先
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?