Last active
December 19, 2019 08:25
-
-
Save pistatium/051f384bfcebc2c0e53573dc8fddec58 to your computer and use it in GitHub Desktop.
Immutable Python スクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Untitled2.ipynb", | |
"provenance": [] | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "c6hlTFHILlZc", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## list と tuple の違い\n", | |
"### list の 場合" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "ZxcUqO1bLTNm", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"l = [1, 2, 3, 4, 5] " | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "It2GVhDrLaLm", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"outputId": "581d924b-8c07-42ea-999f-3dc67942cb44" | |
}, | |
"source": [ | |
"# index を指定して読み取り\n", | |
"l[1] " | |
], | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"2" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 4 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "uqc5QpCQLb9A", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"outputId": "48a23235-876f-45fd-f1b2-33b29ea0cb81" | |
}, | |
"source": [ | |
"# 範囲を指定して取り出し\n", | |
"l[:3] " | |
], | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"[1, 2, 3]" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 5 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "HAv9mZ7oMu7Y", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"outputId": "49a8946e-b1a4-4da5-d7d6-759915327ed8" | |
}, | |
"source": [ | |
"# index を指定して書き換え\n", | |
"l[0] = 6\n", | |
"l" | |
], | |
"execution_count": 13, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"[6, 2, 3, 4, 5]" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 13 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "YqzgszZFM4RT", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"outputId": "a6e45570-e83c-4fcb-e9d1-70642412d6a0" | |
}, | |
"source": [ | |
"# 末尾に追加\n", | |
"l.append(7)\n", | |
"l" | |
], | |
"execution_count": 15, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"[6, 2, 3, 4, 5, 7, 7]" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 15 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "arspcb-uM9pk", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"outputId": "ed390141-75a8-4aed-e2e6-dff18b7055ce" | |
}, | |
"source": [ | |
"# 変数の使い回し\n", | |
"l = [8, 9, 10]\n", | |
"l" | |
], | |
"execution_count": 17, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"[8, 9, 10]" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 17 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "IOnDxHqoLuoP", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### tuple の場合" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "alcq-BFkLyKT", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"t = (1, 2, 3, 4, 5)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "zmsEThIPL1ne", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"outputId": "bfa47803-4668-41fd-e6ee-5883934123ed" | |
}, | |
"source": [ | |
"# index を指定して読み取り\n", | |
"t[1]" | |
], | |
"execution_count": 7, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"2" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 7 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "b0I0_uZUL40V", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"outputId": "262986ab-d55f-49f0-8a45-764312505410" | |
}, | |
"source": [ | |
"# 範囲を指定して取り出し\n", | |
"t[:3]" | |
], | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(1, 2, 3)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 9 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "F1ZVwHYnL6qv", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 164 | |
}, | |
"outputId": "b2e252bb-a4be-4516-80e5-2cba460a59b9" | |
}, | |
"source": [ | |
"# index を指定して書き換え\n", | |
"t[0] = 6" | |
], | |
"execution_count": 18, | |
"outputs": [ | |
{ | |
"output_type": "error", | |
"ename": "TypeError", | |
"evalue": "ignored", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-18-28b8e5df48e8>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m6\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "YUoxJ-VxNHYA", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 164 | |
}, | |
"outputId": "d8f8de5d-4d7a-4fa1-f03f-2ed21b0c489f" | |
}, | |
"source": [ | |
"# 末尾に追加\n", | |
"t.append(7)" | |
], | |
"execution_count": 19, | |
"outputs": [ | |
{ | |
"output_type": "error", | |
"ename": "AttributeError", | |
"evalue": "ignored", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-19-91f1b43b8697>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "8P1bvXWqNKgM", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"outputId": "1262edda-63d9-427c-a25d-19e47d9dcba6" | |
}, | |
"source": [ | |
"# 変数の使い回し\n", | |
"t = [8, 9, 10]\n", | |
"t" | |
], | |
"execution_count": 20, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"[8, 9, 10]" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 20 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "pJIrTIsTNPpP", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "OjoHqr3nNSkF", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## 比較" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "zp5cU7MFNWql", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"outputId": "b487c05a-616f-494d-a3fa-306fc59815b8" | |
}, | |
"source": [ | |
"class Hoge:\n", | |
" def __init__(self):\n", | |
" self.fuga = 'fuga'\n", | |
"\n", | |
"h1 = Hoge()\n", | |
"h2 = Hoge()\n", | |
"h1 == h2" | |
], | |
"execution_count": 21, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"False" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 21 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "M3DsctloNjCi", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"outputId": "5ca161f7-c79a-461d-b5c3-55a889bbb340" | |
}, | |
"source": [ | |
"from typing import NamedTuple\n", | |
"\n", | |
"class Hoge(NamedTuple):\n", | |
" fuga: str = 'fuga'\n", | |
"\n", | |
"h1 = Hoge()\n", | |
"h2 = Hoge()\n", | |
"h1 == h2" | |
], | |
"execution_count": 22, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 22 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "i4d0E1q2NxTT", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## 副作用" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "yewDtu_uN0Ns", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"def print_last(l: list):\n", | |
" \"\"\" Listの一番後ろを表示する関数 \"\"\"\n", | |
" last = l.pop()\n", | |
" print(last)\n", | |
"\n", | |
"l = [1, 2, 3]" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "ohoL77kRN2rI", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"outputId": "03a32a03-2aaf-4aa2-fa49-74f00fb26710" | |
}, | |
"source": [ | |
"print_last(l)" | |
], | |
"execution_count": 24, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"3\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "1qNbLurTN4aq", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"outputId": "b9bf7c24-0391-4717-b433-239a69173a6d" | |
}, | |
"source": [ | |
"l" | |
], | |
"execution_count": 25, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"[1, 2]" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 25 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "btFMW4mgN5Hk", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"def print_last(t : tuple):\n", | |
" \"\"\" tuple バージョン \"\"\"\n", | |
" t = t[-1] \n", | |
" print(t)\n", | |
"\n", | |
"t = (1, 2, 3)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "j8KwfWsQODR7", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"outputId": "d81378f7-a1d6-4d62-dcb4-6da8a69b9460" | |
}, | |
"source": [ | |
"print_last(t)" | |
], | |
"execution_count": 29, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"3\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "vp1hKx3dOMWA", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"outputId": "c5d671bd-2fc4-49c1-efc0-fd277eb1d923" | |
}, | |
"source": [ | |
"t" | |
], | |
"execution_count": 30, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(1, 2, 3)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 30 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "80iu048KOPTs", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## ハッシュ化" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "h6wSG0hQOR4H", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"l = [1, 2, 3] # list\n", | |
"t = (1, 2, 3) # tuple\n", | |
"\n", | |
"d = {}" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "X467p-NOOjnN", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 164 | |
}, | |
"outputId": "16878d3f-8801-4a7f-a23a-747b72701826" | |
}, | |
"source": [ | |
"d[l] = \"これは list です\"" | |
], | |
"execution_count": 32, | |
"outputs": [ | |
{ | |
"output_type": "error", | |
"ename": "TypeError", | |
"evalue": "ignored", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-32-348e43151d5a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0md\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0ml\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"これは list です\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "YKIHLaWwOk5Z", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"outputId": "6d858549-39d3-45d9-c3cb-a337656d75d9" | |
}, | |
"source": [ | |
"d[t] = \"これは tuple です\"\n", | |
"d" | |
], | |
"execution_count": 34, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"{(1, 2, 3): 'これは tuple です'}" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 34 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "US-PpS0fO7nH", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## 構造化したNamedTuple" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "c7RWwowJPASb", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"from typing import Tuple, NamedTuple\n", | |
"\n", | |
"class User(NamedTuple):\n", | |
" name: str\n", | |
"\n", | |
"class Group(NamedTuple):\n", | |
" users: Tuple[User, ...]\n", | |
"\n", | |
"u1 = User(name='taro')\n", | |
"u2 = User(name='hanako')\n", | |
"g = Group(users=(u1, u2))" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "9MWCJmuxPDmn", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
}, | |
"outputId": "239293bd-1064-4c11-dc3e-9b9b4440a823" | |
}, | |
"source": [ | |
"g" | |
], | |
"execution_count": 38, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"Group(users=(User(name='taro'), User(name='hanako')))" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 38 | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment