2
4

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 > array > 個数を決めて初期化する > mylist = [idx for idx in range(10)] / mylist = [0 for idx in range(10)] >> mylist = [0] * 10

Last updated at Posted at 2015-11-30

pythonでサイズNのarrayとして宣言、というのがよくわからなかった。

http://www.i-programmer.info/programming/python/3942-arrays-in-python.html?start=1
このあたりを読んで分かったやり方。

try1

test.py
mylist = [idx for idx in range(10)]
print mylist
結果
Success	time: 0.03 memory: 44680 signal:0
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

これで100の個数のarrayも一応初期化できる。

初期値を0にしたい場合は一番左のidxを0にすればいいようだ。

mylist = [0 for idx in range(10)]

improved

(2015/12/01追記)

初期化だけならコメントでお二方に教えていただいた下記の方法がシンプルで見やすい。

mylist = [0] * 10

range()の注意点などは @shiracamus さんの コメント(2015/12/01 07:41)に詳しいので参照ください。

2
4
3

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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?