0
2

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 5 years have passed since last update.

Python > list > コピー方法3つ > .copy() / list() / [:] > Python3では動く / Python2では.copy()でエラー > 全てshallow copyでした

Last updated at Posted at 2017-02-09

@ Introducing Python: Modern Computing in Simple Packages by Bill Lubanovic
(No. 1424 / 12833)

listのコピーの方法3つあるようだ。

試してみた。

a = [ 'I am clone ' ]
b = a.copy()
c = list(a)
d = a[:]
 
a = [ 'I am original']
print(a)
print(b)
print(c)
print(d)

Python 3

結果
['I am original']
['I am clone ']
['I am clone ']
['I am clone ']

Python 2

結果
Traceback (most recent call last):
  File "prog.py", line 2, in <module>
AttributeError: 'list' object has no attribute 'copy'

docs

Python 3
https://docs.python.org/3.6/tutorial/datastructures.html

Python 2
https://docs.python.org/2/tutorial/datastructures.html

Python 3にはlist.copy()の記述があり、Python 2にはない。

追加で学習した事項

@shiracamus さんにコメントいただいたように、上記のコピーはすべてshallow copyでした。

0
2
4

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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?