{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "(ch:list)=\n", "# リスト" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 基本" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[リスト](https://docs.python.org/ja/3/library/stdtypes.html?highlight=list#sequence-types-list-tuple-range)は複数の値・オブジェクトをまとめる。要素を`,`で区切って並べ、全体を`[` `]`で囲んで作成する。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "a = [0, 1, 4, 9, 16, 25, 36, 49]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 4, 9, 16, 25, 36, 49]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "先頭要素のインデックス番号は0。" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "リストの要素数は[len](https://docs.python.org/ja/3/library/functions.html#len)関数で取得する。" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "要素の範囲を超えてリストにアクセスするとエラーになる。" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "IndexError", "evalue": "list index out of range", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/tmp/ipykernel_2627/767257108.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m8\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list index out of range" ] } ], "source": [ "a[8]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "リストの最後の要素のインデックス番号は「要素数 - 1」。" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "49" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[len(a)-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "リストの要素を末尾からアクセスするため、負のインデックス番号を用いることができる。" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "49" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[-1]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "36" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[-2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "空のリストは`[]`か[list](https://docs.python.org/ja/3/library/stdtypes.html#list)関数で作成する。" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = []\n", "a" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = list()\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## スライス" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`a[start:end:step]`という形式で、リストの要素の部分列を抽出できる(「スライス」と呼ばれる)。\n", "+ `start`: 抽出を開始する要素のインデックス番号。省略された場合は`0`となる。\n", "+ `end`: 抽出を終了する要素のインデックス番号(ただし、`a[end]`の要素は含まれない)。省略された場合はリストの要素数となる。\n", "+ `step`: 要素を抽出する間隔。省略された場合は`1`である。" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "a = [0, 1, 4, 9, 16, 25, 36, 49]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "開始位置と終了位置を指定したスライス。" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 9, 16]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[2:5]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 4]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[0:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "開始位置を省略したスライス。" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 4]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "終了位置を省略したスライス。" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[25, 36, 49]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[5:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "負のインデックス番号を用いてもよい。" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[25, 36, 49]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[-3:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "要素を取り出す間隔を指定したスライス。" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 9, 25, 49]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1::2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "間隔に`-1`を指定して、逆順に要素を取り出すスライス(`a[5]`, `a[4]`, `a[3]`が取り出される)。" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[25, 16, 9]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[5:2:-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "開始位置と終了位置を省略し、全ての要素を逆順に取り出すスライス。" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[49, 36, 25, 16, 9, 4, 1, 0]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[::-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## リストの要素に対する繰り返し" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "a = [0, 1, 4, 9, 16, 25, 36, 49]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "リストの要素に先頭から繰り返しアクセスする。" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n", "25\n", "36\n", "49\n" ] } ], "source": [ "for x in a:\n", " print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "リストの要素に先頭から繰り返しアクセスすると同時に、各要素のインデックス番号を得る。" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 0 0\n", "1 1 1\n", "2 4 4\n", "3 9 9\n", "4 16 16\n", "5 25 25\n", "6 36 36\n", "7 49 49\n" ] } ], "source": [ "for i, x in enumerate(a):\n", " print(i, x, a[i])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "リストの要素に末尾から繰り返しアクセスする。" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "49\n", "36\n", "25\n", "16\n", "9\n", "4\n", "1\n", "0\n" ] } ], "source": [ "for x in reversed(a):\n", " print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## リストの要素に対する操作" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['monday', 'tuesday', 'wednesday', 'thursday', 'friday']" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wd = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']\n", "wd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "リスト内の要素を変更する。" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['monday', 'tue', 'wednesday', 'thursday', 'friday']" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wd[1] = 'tue'\n", "wd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "末尾に要素を追加する。" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "wd = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']\n", "wd.append('saturday')" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "指定した要素を削除する。" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "del wd[5]" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['monday', 'tuesday', 'wednesday', 'thursday', 'friday']" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "リストを連結して、新しいリストを作成する。" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "d = wd + ['saturday', 'sunday']" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "リストをスタックと見なしてpop操作をすると、末尾の要素が取り出され、同時に末尾の要素がリストから削除される。" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'sunday'" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.pop()" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "push操作に相当するものはappend関数である。" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sun']" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.append('sun')\n", "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 要素の並び替え\n", "\n", "要素を降順に並べたリストを新たに作成する(この場合は辞書順に並ぶ)。" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['friday', 'monday', 'saturday', 'sun', 'thursday', 'tuesday', 'wednesday']" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "元のリスト(`d`)は変更されていない。" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sun']" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "引数reverseを`True`にセットすると、要素を昇順に並べたリストを新たに作成できる(この場合は辞書の逆順に並ぶ)。" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['wednesday', 'tuesday', 'thursday', 'sun', 'saturday', 'monday', 'friday']" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(d, reverse=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "並び替えにおける要素の並び順は要素に対する比較演算子`<`の結果に基づく。例えば、`'saturday' < 'sun'`が成り立つので、`'saturday'`が`'sun'`の前に並ぶ。要素を比較するときの基準を変更したいときは、引数keyを用いる。以下の例では、要素の長さ(文字数)を取得する無名関数(lambda)を定義し、それをkey引数に渡すことで、文字数の少ない要素の順に並び替えている。" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['sun', 'monday', 'friday', 'tuesday', 'thursday', 'saturday', 'wednesday']" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(d, key=lambda x: len(x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "引数reverseと併用して、文字数の多い要素の順に並び替える例である。" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['wednesday', 'thursday', 'saturday', 'tuesday', 'monday', 'friday', 'sun']" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(d, key=lambda x: len(x), reverse=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "要素を降順に並び替える。今回は、リスト`d`の要素そのものが並び替えられる。" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['friday', 'monday', 'saturday', 'sun', 'thursday', 'tuesday', 'wednesday']" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.sort()\n", "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "要素を昇順に並び替える。" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['wednesday', 'tuesday', 'thursday', 'sun', 'saturday', 'monday', 'friday']" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.sort(reverse=True)\n", "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "引数keyに関数を指定して、要素の並べ方を変更できる。" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['sun', 'monday', 'friday', 'tuesday', 'thursday', 'saturday', 'wednesday']" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.sort(key=lambda x: len(x))\n", "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 要素の所属検査\n", "\n", "ある要素がリストに含まれるかを調べる。" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'sun' in d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ある要素がリストに含まれないかを調べる。" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'sun' not in d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## リストと参照" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 参照の代入\n", "リスト`x`を作成する。" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['mon', 'tue', 'wed', 'thu', 'fri']" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = ['mon', 'tue', 'wed', 'thu', 'fri']\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "変数`y`を作成し、リストである`x`を代入する。" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['mon', 'tue', 'wed', 'thu', 'fri']" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = x\n", "y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "リスト`y`に要素を追加してみる。" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y.append('sat')\n", "y.append('sun')\n", "y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(予想に反し)リスト`x`の要素も変更されている(`'sat'`と`'sun'`が追加されている)。" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "これは、代入文`y = x`が「変数`y`を定義し、変数`y`の参照先を変数`x`の参照先と一致させる」という動作を行うからである。元々、変数`x`はリストオブジェクトを参照していた(関連付けられていた)ので、そのリスト・オブジェクトに複数の変数`x`と`y`を経由してアクセスできるようになった。ひとつのオブジェクトの実体を複数の変数`x`と`y`が参照している状況であるので、いずれの変数を経由してもオブジェクトの内容を変更できるし、いずれの変数を評価しても同じ結果が得られる。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### リストのコピー\n", "\n", "先ほどと同様に、リスト`x`を作成する。" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['mon', 'tue', 'wed', 'thu', 'fri']" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = ['mon', 'tue', 'wed', 'thu', 'fri']\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "変数`y`を作成し、リストである`x`の全体のスライス`x[:]`を代入する。このとき、`x`のリスト・オブジェクトのコピーが作成され、変数`y`はコピーを参照することになる。" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['mon', 'tue', 'wed', 'thu', 'fri']" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = x[:]\n", "y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "先ほどと同様に、リスト`y`に要素を追加してみる。" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "y.append('sat')\n", "y.append('sun')" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "今回は、リスト`x`の要素は変更されない。これは、変数`x`と`y`が、それぞれ別のリスト・オブジェクトを参照しているからである。" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['mon', 'tue', 'wed', 'thu', 'fri']" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## リストと関数の引数" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "引数で与えられたリストの末尾に`'END'`を追加する関数append_endを定義する。" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "def append_end(s):\n", " s.append('END')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "以下のリスト`x`を引数として、関数append_endを呼び出す。" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['one', 'two']" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = ['one', 'two']\n", "x" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "append_end(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "すると、呼び出し元の変数である`x`の内容が変化する。" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['one', 'two', 'END']" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "数値や文字列を引数として関数に渡す場合と異なり、リストを引数として関数に渡すと、その関数内でそのリストに対する変更は、関数の呼び出し元にも波及する。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## リストの様々な作成方法" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[range](https://docs.python.org/ja/3/library/stdtypes.html#range)関数が表す範囲に対応するリストを作成するには、[list](https://docs.python.org/ja/3/library/stdtypes.html#list)関数を用いる。" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = list(range(10))\n", "a" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 3, 5, 7, 9]" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = list(range(1, 10, 2))\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "リストの要素はデータ型が異なってもよい。" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "b = [1, 'one', 'first']" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b[0]" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'one'" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(0を含む)自然数のリストを[range](https://docs.python.org/ja/3/library/functions.html#func-range)関数で作成する。" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = list(range(10))\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### リストの内包表記" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "空のリストを作成する。" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a2 = []\n", "a2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "空のリスト(`a2`)に要素を追加する" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "for i in range(10):\n", " a2.append(i * 2)\n", "a2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "これは2の段の掛け算の結果を表している。" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "14" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a2[7]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "上記の処理はリストの内包表記を使って簡潔に書ける。ソースコードを左から\"list of i times two for all i in the range of ten\"のように英語読みをすると解釈しやすい。" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a2n = [i * 2 for i in range(10)]\n", "a2n" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "14" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a2n[7]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3の段の掛け算も内包表記を使って簡潔に書ける。" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[i * 3 for i in range(10)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "内包表記を入れ子にして、掛け算表を2次元配列(リスト)として作成する。" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [], "source": [ "m = [[i * j for i in range(10)] for j in range(10)]" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],\n", " [0, 2, 4, 6, 8, 10, 12, 14, 16, 18],\n", " [0, 3, 6, 9, 12, 15, 18, 21, 24, 27],\n", " [0, 4, 8, 12, 16, 20, 24, 28, 32, 36],\n", " [0, 5, 10, 15, 20, 25, 30, 35, 40, 45],\n", " [0, 6, 12, 18, 24, 30, 36, 42, 48, 54],\n", " [0, 7, 14, 21, 28, 35, 42, 49, 56, 63],\n", " [0, 8, 16, 24, 32, 40, 48, 56, 64, 72],\n", " [0, 9, 18, 27, 36, 45, 54, 63, 72, 81]]" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m[2]" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m[2][3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2次元配列作成時の注意" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`*`演算子を使うと、指定した値で初期化しながら、指定した要素数のリストを作ることができる。" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [0] * 10\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "これを拡張して、2次元配列を作ることもできるが、これは避けるべきである。" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [[0] * 10] * 10\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "一見すると2次元配列が作成されたように見えるが、思わぬ落とし穴がある。以下のコードである要素を変更したつもりが、列全体の値が変更されてしまう。" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[0, 0, 0, 0, 0, 2, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 2, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 2, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 2, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 2, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 2, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 2, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 2, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 2, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 2, 0, 0, 0, 0]]" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[3][5] = 2\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "これは、`[0] * 10`で1次元のリスト・オブジェクトが一つだけ作成され、その唯一のオブジェクトへの参照が全ての`a[.]`にセットされただけであるからである。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "指定したサイズのリストを作成するときに、内包表記を使うこともできる。" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = [0 for i in range(10)]\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "これを拡張して、2次元配列を作ることができる。" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = [[0 for i in range(10)] for j in range(10)]\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "こちらは、我々が通常期待する2次元配列として振る舞う。" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 2, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b[3][5] = 2\n", "b" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "remove-cell" ] }, "source": [ "---\n", "\n", "[Python早見帳](https://chokkan.github.io/python/) © Copyright 2020-2022 by [岡崎 直観 (Naoaki Okazaki)](https://www.chokkan.org/). この作品はクリエイティブ・コモンズ 表示 - 非営利 - 改変禁止 4.0 国際 ライセンスの下に提供されています。\"クリエイティブ・コモンズ・ライセンス\"" ] } ], "metadata": { "@context": { "CreativeWork": "http://schema.org/CreativeWork", "Organization": "http://schema.org/Organization", "Person": "http://schema.org/Person", "author": "http://schema.org/author", "copyrightHolder": "http://schema.org/copyrightHolder", "copyrightYear": "http://schema.org/copyrightYear", "license": "http://schema.org/license", "name": "http://schema.org/name", "title": "http://schema.org/name", "url": "http://schema.org/url" }, "@type": "CreativeWork", "author": [ { "@type": "Person", "name": "Naoaki Okazaki", "url": "https://www.chokkan.org/" } ], "copyrightHolder": [ { "@type": "Person", "name": "Naoaki Okazaki", "url": "https://www.chokkan.org/" } ], "copyrightYear": 2022, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" }, "license": "https://creativecommons.org/licenses/by-nc-nd/4.0/deed.ja", "title": "Python早見帳" }, "nbformat": 4, "nbformat_minor": 4 }