LoginSignup
1
1

More than 3 years have passed since last update.

リストオブジェクトの共有

Last updated at Posted at 2019-05-03

はじめに(5月4日追記)

コメント欄の @shiracamus さんのご説明が非常に分かりやすく参考になると思います.
この記事の本文よりも,是非コメント欄をお読みください.

自己紹介

データサイエンスに関する研究(理論と応用)をしている学生です.
これまでR言語しか使ってきませんでしたが,最近Pythonの学習を始めました.
RとPythonで全然違うなあと思った点や,使い分ける上で注意すべき点をまとめていきます.
今後,ご指摘やアドバイスなど,よろしくお願いいたします.

たとえば

test.R
lis1 = list(1,2)
lis2 = lis1
lis1[[1]] = 100
print(lis2)

# 出力
[[1]]
[1] 1

[[2]]
[1] 2

Pythonだと,全然違う結果になる.
参照渡しが行われている.

test.py

lis1 = [1,2]
lis2 = lis1
lis1[0] = 100
print(lis2)

# 出力
[100, 2]

Rっぽく扱う(値渡し)をするためには,copyモジュールとやらを使えばよさそう.

1
1
18

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
1
1