リスト
リスト(List)とはPythonの組み込み型の1つで、シーケンス(sequence)の一種です。他の言語におけるインデックス配列に似ていますが、かなり柔軟な操作ができます。このページではリストをつかった操作を、出来る限り網羅的に紹介します。
リストの生成
リストは、大括弧[]の中にカンマ区切りで値を並べることで生成できます。整数だけでなく文字列やリストなどを含むこともできます。
1 2 3 4 5 6 7 8 9 | >>> listA = [0,1,2,3,4] >>> listA [0, 1, 2, 3, 4] >>> listB = ["A","B","C"] >>> listB ['A', 'B', 'C'] >>> listC = [listA, listB] >>> listC [[0, 1, 2, 3, 4], ['A', 'B', 'C']] |
インデクシング
Pythonのインデックス指定はかなり柔軟です。
- 負の数を指定できる
- 範囲指定ができる
- とび数(step)を指定できる
- 指定を省略できる
基本
リストの後ろに大括弧で囲んだ数字を書くことで、任意の要素を取り出せます。
1 2 3 4 5 6 | >>> listA[0] 0 >>> listA[1] 1 >>> listA[4] 4 |
負の数を指定
後ろからの位置指定になります。
1 2 3 4 5 6 | >>> listA[-1] 4 >>> listA[-2] 3 >>> listA[-3] 2 |
範囲指定
コロン区切りで開始位置と終了位置を指定します。終了位置は1つ先のインデックスを指定することに注意しましょう。
1 2 3 4 5 6 7 8 | >>> listA[1:4] [1, 2, 3] >>> listA[0:5] [0, 1, 2, 3, 4] >>> listA[2:3] [2] >>> listA[3:3] [] |
飛び数指定(step指定)
コロン区切りで3番目の数字が飛び数(step)です。
1 2 3 4 5 6 7 8 9 10 11 | >>> listA[0:5:1] [0, 1, 2, 3, 4] >>> listA[0:5:2] [0, 2, 4] >>> listA[0:5:3] [0, 3] >>> listA[0:5:4] [0, 4] >>> listA[0:5:5] [0] >>> |
指定の省略
コロン区切りで3つの値を指定できますが、すべての要素は省略できます。
1 2 3 4 5 6 | >>> listA[::-1] [4, 3, 2, 1, 0] >>> listA[::-2] [4, 2, 0] >>> listA[:3] [0, 1, 2] |
リストの複製
listAとlistA[:]は同じ値になりますが、異なるオブジェクトを挿しています。リストの末尾に[:]と記述することで、リストの複製(clone)を生成できます。
1 2 3 4 5 6 7 8 9 | >>> listA [0, 1, 2, 3, 4] >>> listZ = listA[:] >>> listZ [0, 1, 2, 3, 4] >>> listA == listZ True >>> listA is listZ False |
多次元配列へのアクセス
インデックス指定を連続して記述することで多次元配列にアクセスできます。
1 2 3 4 5 6 7 8 9 10 | >>> listC [[0, 1, 2, 3, 4], ['A', 'B', 'C']] >>> listC[0] >>> listC[1] ['A', 'B', 'C'] [0, 1, 2, 3, 4] >>> listC[0][0] 0 >>> listC[1][2] 'C' |
リストへの代入
代入の左辺にリストをおくことでリストの要素を置換できます。
単一要素を置換
一般的な代入です。要素数の自動拡張は行われません。
1 2 3 4 5 6 7 | >>> listA[0] = 100 >>> listA [100, 1, 2, 3, 4] >>> listA[5] = 100 Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list assignment index out of range |
複数要素を置換
左辺でインデックスの範囲を指定することで、要素をまとめて置換したり、取り除いたり、新しい要素を追加したりできます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | >>> listA = [0,1,2,3,4] >>> listA[1:4] = [101,102,103] >>> listA [0, 101, 102, 103, 4] >>> listA[1:4] = [] >>> listA [0, 4] >>> listA[1:1] = ["A","B","C"] >>> listA [0, 'A', 'B', 'C', 4] >>> listA = [1,1,1,1,1] >>> listA[::2] = [2,2,2] >>> listA [2, 1, 2, 1, 2] |
要素を先頭に追加
1 2 3 4 5 6 7 | >>> listA = [0,0,0,0,0] >>> listA[0:0] = [1] >>> listA [1, 0, 0, 0, 0, 0] >>> listA[0:0] = [2,2,2] >>> listA [2, 2, 2, 1, 0, 0, 0, 0, 0] |
要素を末尾に追加
1 2 3 4 5 6 7 8 | >>> listA = [0,0,0] >>> listA.append(2) >>> listA [0, 0, 0, 2] >>> listA.extend([4,4,4]) >>> listA [0, 0, 0, 2, 4, 4, 4] >>> |
リストの結合
リストは、シーケンスの一種なので+演算子で結合できます。また、文字列と同様に、*演算子で繰り返すことができます。
1 2 3 4 5 6 7 | >>> listA + listA [0, 1, 2, 3, 4, 0, 1, 2, 3, 4] >>> listA * 3 [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4] 'hogehoge' #文字列もシーケンスの一種 >>> "hoge"*5 'hogehogehogehogehoge' |
リストの反転
step:-1のインデクシングをするか、reverseメソッドを使うことで反転できます。
1 2 3 4 5 6 7 | >>> listA = [1,2,3,4,5] >>> listA = listA[::-1] >>> listA [5, 4, 3, 2, 1] >>> listA.reverse() >>> listA [1, 2, 3, 4, 5] |
listA.reverse()は値を返さないことに注意。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | >>> for num in listA[::-1]: ... num ... 5 4 3 2 1 >>> for num in listA.reverse(): ... num ... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'NoneType' object is not iterable |
アンパック代入
リストの要素をまとめて代入できます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | >>> x,y = 1,2 >>> x 1 >>> y 2 >>> x,y=y,x >>> x 2 >>> y 1 >>> x,y = [y,x] #一時変数を使わずに値を入れ替える >>> (x,y) = [y,x] >>> [x,y] = [y,x] >>> [x,y] = y,x |
値を取り除く(remove)
removeで特定の値を除去できます。
- 同じ値が複数含まれている場合は先頭の値だけ除去します。
- 指定した値が含まれていない場合は例外が発生します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | >>> listA = [1,1,1,2,2,3] >>> listA.remove(1) >>> listA [1, 1, 2, 2, 3] >>> listA.remove(1) >>> listA [1, 2, 2, 3] >>> listA.remove(1) >>> listA [2, 2, 3] >>> listA.remove(1) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: list.remove(x): x not in list >>> listA.remove(3) >>> listA [2, 2] |
最後の要素を取り除く(pop)
最後の要素を取り出します。
- リスト自体が変化します。
- 空リストに対して実行すると例外が発生します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | >>> listA = [1,2,3,4,5] >>> listA.pop() 5 >>> listA.pop() 4 >>> listA.pop() 3 >>> listA.pop() 2 >>> listA.pop() 1 >>> listA.pop() Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: pop from empty list |
サイズ(要素数)を求める
1 2 3 4 5 6 7 | >>> listA = [1,2,3,4,5] >>> len(listA) 5 >>> listA.pop() 5 >>> len(listA) 4 |
値の存在確認
見つかればTrue、見つからなければFalseになる。
1 2 3 4 | >>> 1 in [1,2,3] True >>> 4 in [1,2,3] False |
値の出現位置を調べる
見つからない場合は例外が発生する。
1 2 3 4 5 6 7 8 9 10 | >>> listB = ["A","B","C","C"] >>> listB.index("B") 1 >>> listB.index("C") 2 >>> listB.index("D") Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: list.index(x): x not in list >>> |
リストの大小関係
先頭から順に比較。混乱の原因になるので、使うべきでないと思う。
1 2 3 4 5 6 7 8 9 10 | >>> [1,2,3] < [1,2,3] False >>> [1,2,3] <= [1,2,3] True >>> [1,2,3] < [1,2,4] True >>> [1,2,3] < [1,2] False >>> [1,2,3] < [1,3,2] True |
文字列のリストを結合
結合したり分解したり。
1 2 3 4 5 6 | >>> listB ['A', 'B', 'C', 'C'] >>> " ".join(listB) 'A B C C' >>> "A,B,C,D,E".split(",") ['A', 'B', 'C', 'D', 'E'] |