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?

Python3お勉強メモ(リストの繰り返し処理)

Posted at

リストの繰り返し処理にもいくつかやり方があるみたいなので整理

基本の繰り返し

リストを参照するだけならこの書き方で問題なさそう

li = [1, 2, 3]
for a in li:
    print(a)
インデックスで繰り返し

組み込み関数enumerate()を使う形
https://docs.python.org/ja/3/library/functions.html#enumerate

要素だけでなく、インデックスも欲しい場合に

li = [1, 2, 3]
for i,a in enumerate(li):
    print(i,a)
インデックスで繰り返し(要素の変更あり)

range()とlen()を組み合わせてC言語のようなインデックスで参照する形
要素の変更が伴う場合に

li = [1, 2, 3]
for i in range(len(li)):
    li[i] = i*2
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?