{ "cells": [ { "cell_type": "markdown", "id": "002aa41b-86fc-495f-9235-e0ed656d67a4", "metadata": {}, "source": [ "## 標準モジュール" ] }, { "cell_type": "markdown", "id": "a7fa07f6-9519-4563-8d84-220b01858447", "metadata": {}, "source": [ "### [math --- 数学関数](https://docs.python.org/ja/3/library/math.html)" ] }, { "cell_type": "code", "execution_count": 27, "id": "93b6dbbe-2375-4bf5-863f-1f7a821f27bf", "metadata": {}, "outputs": [], "source": [ "import math" ] }, { "cell_type": "markdown", "id": "f860f8a0-fe2a-410a-9750-d49acdb566d6", "metadata": {}, "source": [ "$e^2$" ] }, { "cell_type": "code", "execution_count": 29, "id": "f8ca2d01-b51b-4e66-b005-e0d0298651db", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7.38905609893065" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.exp(2)" ] }, { "cell_type": "markdown", "id": "be7953fa-eef0-4a83-8797-27b56cb40ce0", "metadata": {}, "source": [ "$2^6$" ] }, { "cell_type": "code", "execution_count": 30, "id": "cf57db4d-f892-49f0-ad98-d1b6d5f541f5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "64.0" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.pow(2, 6)" ] }, { "cell_type": "markdown", "id": "6d4264e9-e774-4761-8cb2-325c87b90c2b", "metadata": {}, "source": [ "$\\log 7.38905609893065$" ] }, { "cell_type": "code", "execution_count": 49, "id": "b52ba341-9010-4110-91ae-a04aed141f21", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.log(7.38905609893065)" ] }, { "cell_type": "markdown", "id": "f5abdc3b-25ef-4fa6-ab00-e6670ae35481", "metadata": {}, "source": [ "$\\log_2 64$" ] }, { "cell_type": "code", "execution_count": 32, "id": "f8cf2ea7-0769-449a-b2f7-5649e880f175", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6.0" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.log(64, 2)" ] }, { "cell_type": "markdown", "id": "d58b312d-9927-426a-88d2-14ca67dfbea5", "metadata": {}, "source": [ "$\\sqrt{25}$" ] }, { "cell_type": "code", "execution_count": 33, "id": "2592c1fb-4e35-4da8-b887-f278ac0d11b9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.sqrt(25)" ] }, { "cell_type": "markdown", "id": "c91035fa-adeb-4329-b1ef-c47be28ff791", "metadata": {}, "source": [ "$\\sin \\frac{\\pi}{4}$" ] }, { "cell_type": "code", "execution_count": 40, "id": "1a526e8c-76ca-4d31-b3b1-2f8fa78df0f0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.7071067811865475" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.sin(0.25 * math.pi)" ] }, { "cell_type": "markdown", "id": "86acf074-eb47-4e4f-85d4-965c9c62d661", "metadata": {}, "source": [ "$\\cos \\frac{\\pi}{4}$" ] }, { "cell_type": "code", "execution_count": 41, "id": "863cb4a9-d9e9-4987-af9d-8667aeb7d2a8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.7071067811865476" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.cos(0.25 * math.pi)" ] }, { "cell_type": "markdown", "id": "e2f34c8d-c63a-4a07-9852-404d64ab78b6", "metadata": {}, "source": [ "$\\tan \\frac{\\pi}{4}$" ] }, { "cell_type": "code", "execution_count": 39, "id": "51e61375-724d-4877-a486-71a8be4649be", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.9999999999999999" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.tan(0.25 * math.pi)" ] }, { "cell_type": "markdown", "id": "ed2df393-98b9-4f02-aea7-054f88ec082d", "metadata": {}, "source": [ "$|-2.5|$" ] }, { "cell_type": "code", "execution_count": 48, "id": "7f17ed62-5aac-4779-8a0c-ac53057077d0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.5" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.fabs(-2.5)" ] }, { "cell_type": "markdown", "id": "33074e18-02f8-4c7b-866b-385c1a20610f", "metadata": {}, "source": [ "$\\lfloor -2.5 \\rfloor$" ] }, { "cell_type": "code", "execution_count": 46, "id": "db1bf995-21eb-477e-a1aa-a23867210840", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-3" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.floor(-2.5)" ] }, { "cell_type": "markdown", "id": "e7f1125e-1dbb-4e4a-9db3-2da7c8bb440e", "metadata": {}, "source": [ "$\\lceil -2.5 \\rceil$" ] }, { "cell_type": "code", "execution_count": 47, "id": "4fa66ecb-3456-420c-afe4-4c67393c2232", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-2" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.ceil(-2.5)" ] }, { "cell_type": "markdown", "id": "81adb447-b860-4984-9e0b-5b1eea6d1bde", "metadata": {}, "source": [ "### [random --- 擬似乱数を生成する](https://docs.python.org/ja/3/library/random.html)" ] }, { "cell_type": "code", "execution_count": 5, "id": "a229938a-ee41-4e5d-96ab-bc446e01c2fa", "metadata": { "tags": [] }, "outputs": [], "source": [ "import random" ] }, { "cell_type": "markdown", "id": "55ca79bf-d811-446e-8584-2b4370d1dd64", "metadata": {}, "source": [ "乱数生成器のシードを設定する(生成される乱数の再現性を担保するためにも用いられる)。" ] }, { "cell_type": "code", "execution_count": 6, "id": "94e27078-ffa0-436f-9500-6bb17686ceb8", "metadata": {}, "outputs": [], "source": [ "random.seed(1)" ] }, { "cell_type": "markdown", "id": "d6870a38-b18e-47ff-bca8-2008f6cbc829", "metadata": {}, "source": [ "1から10までの範囲で整数の乱数を生成。" ] }, { "cell_type": "code", "execution_count": 7, "id": "a77423d5-d827-42f9-ad05-3cb9e8923d57", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.randint(1, 10)" ] }, { "cell_type": "markdown", "id": "672cf261-fbfe-401c-b446-88371a83dd0f", "metadata": {}, "source": [ "0から1の範囲の一様分布からランダムに値を取り出す。" ] }, { "cell_type": "code", "execution_count": 8, "id": "e919424d-bdf1-4652-abb1-f508599d4f71", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "0.5692038748222122" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.random()" ] }, { "cell_type": "markdown", "id": "970c94e1-5264-449e-aea8-83070bfd2b0d", "metadata": {}, "source": [ "1から10の範囲の一様分布からランダムに値を取り出す。" ] }, { "cell_type": "code", "execution_count": 11, "id": "bf94132f-4322-4411-bbf1-6c958da557ba", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7.84866200421318" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.uniform(1, 10)" ] }, { "cell_type": "markdown", "id": "89188d11-3c14-45dd-9357-604af5da1928", "metadata": {}, "source": [ "平均50、標準偏差50の正規分布からランダムに値を取り出す。" ] }, { "cell_type": "code", "execution_count": 10, "id": "a5b372aa-d087-4b1c-a2c6-88ba2e3ccb0a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "61.55187661776456" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.gauss(50, 25)" ] }, { "cell_type": "markdown", "id": "ca1b37de-09bc-4d78-9c18-d04f6dc4cc80", "metadata": {}, "source": [ "トランプのカードを表すリストを作成する。" ] }, { "cell_type": "code", "execution_count": 62, "id": "757f6f17-ce13-44bf-b289-b9876eabd173", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cards = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']\n", "cards" ] }, { "cell_type": "markdown", "id": "9fc7069f-eb23-49a9-9a82-d5c75f6c613c", "metadata": {}, "source": [ "リストから一つの要素をランダムに取り出す。" ] }, { "cell_type": "code", "execution_count": 58, "id": "1a634c6d-7b60-4643-a345-200611131a00", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1'" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.choice(cards)" ] }, { "cell_type": "markdown", "id": "c7bbcce9-adaa-4d98-a73e-11ff0e42b8b9", "metadata": {}, "source": [ "リストから要素を3個取り出す。" ] }, { "cell_type": "code", "execution_count": 59, "id": "c9eee07f-166e-4e8b-9194-96ddeba86977", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['7', 'K', '10']" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.sample(cards, 3)" ] }, { "cell_type": "markdown", "id": "c9473020-c589-4376-844a-dffbc8b7c5a6", "metadata": {}, "source": [ "リストの要素をランダムに並び替える。" ] }, { "cell_type": "code", "execution_count": 60, "id": "15abb61c-154b-4889-85b1-af4ade18f589", "metadata": {}, "outputs": [], "source": [ "random.shuffle(cards)\n", "cards" ] }, { "cell_type": "markdown", "id": "ee796ad5-4464-41fc-bd12-bc7614b2babe", "metadata": {}, "source": [ "### [operator --- 関数形式の標準演算子](https://docs.python.org/ja/3/library/operator.html)" ] }, { "cell_type": "code", "execution_count": 49, "id": "0e26ba84-0c7b-4b87-947e-dad6b5d07d12", "metadata": {}, "outputs": [], "source": [ "import operator" ] }, { "cell_type": "markdown", "id": "88988e1c-f7c8-4852-94db-5f6b304d5d4a", "metadata": {}, "source": [ "以下のデータを整列させたい。" ] }, { "cell_type": "code", "execution_count": 50, "id": "0d03e396-8225-4a8d-a6c8-03990cc28dee", "metadata": {}, "outputs": [], "source": [ "D = [\n", " ['Gunma', 6362, 1919014],\n", " ['Tochigi', 6408, 1927754],\n", " ['Ibaraki', 6097, 2849494],\n", "]" ] }, { "cell_type": "markdown", "id": "7b3d2341-c4e6-4006-9fa5-ac185aaa7823", "metadata": {}, "source": [ "県名の辞書順でソートするために、引数keyにソートで用いる要素を取り出す関数を与える。" ] }, { "cell_type": "code", "execution_count": 51, "id": "3bd51689-c74a-48a8-b90a-d484a39f2bee", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['Gunma', 6362, 1919014],\n", " ['Ibaraki', 6097, 2849494],\n", " ['Tochigi', 6408, 1927754]]" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(D, key=lambda x: x[0])" ] }, { "cell_type": "markdown", "id": "929c65ba-b57d-4ec0-82ef-f12b51e7b5b4", "metadata": {}, "source": [ "これは、operator.itemgetterを使って以下のように書ける。" ] }, { "cell_type": "code", "execution_count": 52, "id": "cf8dcc6c-aa23-4a84-90b7-3e46e05d7871", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['Gunma', 6362, 1919014],\n", " ['Ibaraki', 6097, 2849494],\n", " ['Tochigi', 6408, 1927754]]" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(D, key=operator.itemgetter(0))" ] }, { "cell_type": "markdown", "id": "06adaf03-304c-48ab-a59d-654a4cce43a2", "metadata": {}, "source": [ "今度は、リストの要素が辞書オブジェクトとなっている以下のデータを整列させたい。" ] }, { "cell_type": "code", "execution_count": 53, "id": "7bac13aa-17ea-4103-9d77-3d63ce6f98b4", "metadata": {}, "outputs": [], "source": [ "D = [ \n", " {'name': 'Gunma', 'area': 6362, 'population': 1919014},\n", " {'name': 'Tochigi', 'area': 6408, 'population': 1927754},\n", " {'name': 'Ibaraki', 'area': 6097, 'population': 2849494},\n", "]" ] }, { "cell_type": "markdown", "id": "d042324d-770d-47e0-9b70-ca1f59533404", "metadata": {}, "source": [ "面積の小さい順でソートするために、引数keyにソートで用いる要素を取り出す関数を与える。" ] }, { "cell_type": "code", "execution_count": 54, "id": "baf24234-a75e-485f-b114-76f62c893c0d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'name': 'Ibaraki', 'area': 6097, 'population': 2849494},\n", " {'name': 'Gunma', 'area': 6362, 'population': 1919014},\n", " {'name': 'Tochigi', 'area': 6408, 'population': 1927754}]" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(D, key=lambda x: x['area'])" ] }, { "cell_type": "markdown", "id": "e9bfcfda-73a8-421a-91ea-cf9e6525e046", "metadata": {}, "source": [ "これは、operator.itemgetterを使って以下のように書ける。" ] }, { "cell_type": "code", "execution_count": 55, "id": "7a97a10c-04c1-4686-a905-23cf4c523567", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'name': 'Ibaraki', 'area': 6097, 'population': 2849494},\n", " {'name': 'Gunma', 'area': 6362, 'population': 1919014},\n", " {'name': 'Tochigi', 'area': 6408, 'population': 1927754}]" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(D, key=operator.itemgetter('area'))" ] }, { "cell_type": "markdown", "id": "fef69fc6-adbf-4307-ac20-78f42a2470ba", "metadata": {}, "source": [ "今度は、リストの要素がクラスのオブジェクトとなっている以下のデータを整列させたい。" ] }, { "cell_type": "code", "execution_count": 57, "id": "9d1ab8dc-9f11-4bf6-81d1-5afa05c5399a", "metadata": {}, "outputs": [], "source": [ "class Prefecture:\n", " def __init__(self, name='', area=0, population=0):\n", " self.name = name\n", " self.area = area\n", " self.population = population\n", " \n", " def __repr__(self):\n", " return \"Prefecture('{}', {}, {})\".format(self.name, self.area, self.population)\n", "\n", "D = [\n", " Prefecture('Gunma', 6362, 1919014),\n", " Prefecture('Tochigi', 6408, 1927754),\n", " Prefecture('Ibaraki', 6097, 2849494)\n", "]" ] }, { "cell_type": "markdown", "id": "b78a5ec8-abb6-41fb-a75e-efc9eb919f19", "metadata": {}, "source": [ "面積の小さい順でソートするために、引数keyにソートで用いる要素を取り出す関数を与える。" ] }, { "cell_type": "code", "execution_count": 59, "id": "074555c1-589e-48d9-b68e-3084103b1acc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Prefecture('Ibaraki', 6097, 2849494),\n", " Prefecture('Gunma', 6362, 1919014),\n", " Prefecture('Tochigi', 6408, 1927754)]" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(D, key=lambda x: x.area)" ] }, { "cell_type": "markdown", "id": "7a7f67d1-6007-4553-87c5-8b32e486ef4e", "metadata": {}, "source": [ "これは、operator.attrgetterを使って以下のように書ける。" ] }, { "cell_type": "code", "execution_count": 58, "id": "41787f6f-3582-406d-81db-dd6913bfa7eb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Prefecture('Ibaraki', 6097, 2849494),\n", " Prefecture('Gunma', 6362, 1919014),\n", " Prefecture('Tochigi', 6408, 1927754)]" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(D, key=operator.attrgetter('area'))" ] }, { "cell_type": "markdown", "id": "ef804fb0-029a-4efb-a4d9-ad88fc2f71e3", "metadata": {}, "source": [ "### [functools --- 高階関数と呼び出し可能オブジェクトの操作](https://docs.python.org/ja/3/library/functools.html)\n", "\n" ] }, { "cell_type": "code", "execution_count": 9, "id": "456ecae6-942e-4516-9c37-164576eb3a22", "metadata": {}, "outputs": [], "source": [ "import functools" ] }, { "cell_type": "code", "execution_count": 10, "id": "94d59bfe-250b-494b-803e-6f11f2682c95", "metadata": {}, "outputs": [], "source": [ "def w(n=3, c='w'):\n", " for i in range(n):\n", " print(c, end='')" ] }, { "cell_type": "code", "execution_count": 11, "id": "7d52cf6a-614e-445d-a1a6-9a05ae9b0c66", "metadata": {}, "outputs": [], "source": [ "fu = functools.partial(w, c='フ')" ] }, { "cell_type": "code", "execution_count": 12, "id": "2073ff9b-bc11-4662-a4aa-2c976b4692cf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "フフフ" ] } ], "source": [ "fu(3)" ] }, { "cell_type": "code", "execution_count": 13, "id": "543938a0-52df-4797-a6d0-3188d44b5b71", "metadata": {}, "outputs": [], "source": [ "笑 = functools.partial(w, c='笑')" ] }, { "cell_type": "code", "execution_count": 14, "id": "af1f45cd-48c3-4a64-a4aa-32d54533a9b2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "笑笑笑笑笑" ] } ], "source": [ "笑(5)" ] }, { "cell_type": "markdown", "id": "c57559c9-af17-4217-b1e1-a63dc20e94dd", "metadata": {}, "source": [ "### [itertools --- 効率的なループ実行のためのイテレータ生成関数](https://docs.python.org/ja/3/library/itertools.html)" ] }, { "cell_type": "code", "execution_count": 17, "id": "16638f9c-97f7-4d6f-9d1b-2f02f418d111", "metadata": {}, "outputs": [], "source": [ "import itertools" ] }, { "cell_type": "code", "execution_count": 16, "id": "0dfd2410-a649-4052-8c57-6ba84d1be8ab", "metadata": {}, "outputs": [], "source": [ "s = 'para para paradise'" ] }, { "cell_type": "code", "execution_count": 18, "id": "d300f978-4716-4a21-b2bc-f15a241c679c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "p \n", "a \n", "r \n", "a \n", " \n", "p \n", "a \n", "r \n", "a \n", " \n", "p \n", "a \n", "r \n", "a \n", "d \n", "i \n", "s \n", "e \n" ] } ], "source": [ "for c, n in itertools.groupby(s):\n", " print(c, n)" ] }, { "cell_type": "markdown", "id": "37c70e27-c280-446a-ae40-e6fdfb15d618", "metadata": {}, "source": [ "### [collections --- コンテナデータ型](https://docs.python.org/ja/3/library/collections.html)" ] }, { "cell_type": "code", "execution_count": 15, "id": "e2a80042-9c42-4875-ab9c-95d390ee1d4e", "metadata": {}, "outputs": [], "source": [ "import collections" ] }, { "cell_type": "markdown", "id": "be34b82d-d6cc-4006-a7c4-4990396a4ea6", "metadata": {}, "source": [ "### [heapq --- ヒープキューアルゴリズム](https://docs.python.org/ja/3/library/heapq.html)" ] }, { "cell_type": "markdown", "id": "8cee8371-69a1-4f19-a6b2-5dc54f633c70", "metadata": {}, "source": [ "### [bisect --- 配列二分法アルゴリズム](https://docs.python.org/ja/3/library/bisect.html)" ] }, { "cell_type": "markdown", "id": "d7df5b39-3b54-4d80-81fa-0999841fec69", "metadata": {}, "source": [ "### [copy --- 浅いコピーおよび深いコピー操作](https://docs.python.org/ja/3/library/copy.html)" ] }, { "cell_type": "markdown", "id": "9fbf3c37-c394-46ef-9b40-ee9e9ef6b81d", "metadata": {}, "source": [ "### [re --- 正規表現操作](https://docs.python.org/ja/3/library/re.html)" ] }, { "cell_type": "markdown", "id": "33ddf958-47c3-4378-89a6-84b53865a3dd", "metadata": {}, "source": [ "### [datetime --- 基本的な日付型および時間型](https://docs.python.org/ja/3/library/datetime.html)" ] }, { "cell_type": "code", "execution_count": 1, "id": "ae5bdfa9-4d8d-433b-9fdf-1d0d7e0e456e", "metadata": {}, "outputs": [], "source": [ "import datetime" ] }, { "cell_type": "code", "execution_count": 2, "id": "dc31c949-9a68-432b-8895-2ff4c834db3c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2021, 4, 27, 21, 40, 46, 159665)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "datetime.datetime.now()" ] }, { "cell_type": "code", "execution_count": 5, "id": "d754c106-0afe-46d5-a7bc-f16fbc40c23b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2021, 4, 1, 0, 0)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "datetime.datetime.fromisoformat('2021-04-01T00:00:00')" ] }, { "cell_type": "code", "execution_count": 8, "id": "c5862724-fb18-4d4f-bd7e-da6bc3f962e4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2021, 4, 1, 12, 22, 33)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "datetime.datetime.strptime('2021年4月1日12時22分33秒', '%Y年%m月%d日%H時%M分%S秒')" ] }, { "cell_type": "code", "execution_count": 20, "id": "00b0bf6f-36b6-4ad4-8444-bf55d42f682c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2021-04-29T09:26:16.397940'" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt = datetime.datetime.now()\n", "dt.isoformat()" ] }, { "cell_type": "code", "execution_count": 21, "id": "d0e543df-3f9a-462c-91b3-feb2510f312b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2021年04月29日09時26分16秒'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt.strftime('%Y年%m月%d日%H時%M分%S秒')" ] }, { "cell_type": "code", "execution_count": 23, "id": "cc5e8b35-d876-4399-bb74-f7fe897a84b5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.timedelta(days=118, seconds=34158, microseconds=695087)" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt0 = datetime.datetime(2021, 1, 1)\n", "now = datetime.datetime.now()\n", "delta = now - dt0\n", "delta" ] }, { "cell_type": "code", "execution_count": 24, "id": "a4bf43ea-1129-4294-959d-6a47433cfa27", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2021, 5, 29, 9, 30, 11, 985605)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "now = datetime.datetime.now()\n", "delta = datetime.timedelta(days=30)\n", "now + delta" ] }, { "cell_type": "markdown", "id": "092b8918-cb30-4261-81b2-8b4927289065", "metadata": {}, "source": [ "### [csv --- CSV ファイルの読み書き](https://docs.python.org/ja/3/library/csv.html)" ] }, { "cell_type": "code", "execution_count": 52, "id": "745e94aa-74cb-4c0a-9553-716c14a4e1ae", "metadata": {}, "outputs": [], "source": [ "import csv" ] }, { "cell_type": "code", "execution_count": 53, "id": "298e74d3-7297-43a3-b8ca-1a8d37d8773c", "metadata": {}, "outputs": [], "source": [ "D = {\n", " '東京': {'yomi': 'とうきょう', 'en': 'Tokyo'},\n", " '神奈川': {'yomi': 'かながわ', 'en': 'Kanagawa'},\n", " '千葉': {'yomi': 'ちば', 'en': 'Chiba'},\n", " '埼玉': {'yomi': 'さいたま', 'en': 'Saitama'}\n", "}" ] }, { "cell_type": "code", "execution_count": 56, "id": "68295e24-a401-46b9-9001-f5c95d92aaa1", "metadata": {}, "outputs": [], "source": [ "with open('sample.csv', 'w') as fo:\n", " writer = csv.writer(fo)\n", " for pref, info in D.items():\n", " writer.writerow((pref, info['yomi'], info['en']))" ] }, { "cell_type": "code", "execution_count": 57, "id": "50144d3d-bb3d-4e1d-b5dc-7df7e81f03d2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "東京,とうきょう,Tokyo\n", "神奈川,かながわ,Kanagawa\n", "千葉,ちば,Chiba\n", "埼玉,さいたま,Saitama\n" ] } ], "source": [ "%cat sample.csv" ] }, { "cell_type": "markdown", "id": "ba309483-8a80-485b-9211-ce126e592f8d", "metadata": {}, "source": [ "### [json --- JSON エンコーダおよびデコーダ](https://docs.python.org/ja/3/library/json.html)" ] }, { "cell_type": "code", "execution_count": 25, "id": "83f9f99c-c965-492c-9619-59779b2af633", "metadata": {}, "outputs": [], "source": [ "import json" ] }, { "cell_type": "code", "execution_count": 26, "id": "bd77f329-bede-46d8-88fe-08ac02724460", "metadata": {}, "outputs": [], "source": [ "D = {\n", " '東京': {'yomi': 'とうきょう', 'en': 'Tokyo'},\n", " '神奈川': {'yomi': 'かながわ', 'en': 'Kanagawa'},\n", " '千葉': {'yomi': 'ちば', 'en': 'Chiba'},\n", " '埼玉': {'yomi': 'さいたま', 'en': 'Saitama'}\n", "}" ] }, { "cell_type": "code", "execution_count": 31, "id": "00771662-c3b6-4ea9-ba87-1baa0c67a21e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"\\u6771\\u4eac\": {\"yomi\": \"\\u3068\\u3046\\u304d\\u3087\\u3046\", \"en\": \"Tokyo\"}, \"\\u795e\\u5948\\u5ddd\": {\"yomi\": \"\\u304b\\u306a\\u304c\\u308f\", \"en\": \"Kanagawa\"}, \"\\u5343\\u8449\": {\"yomi\": \"\\u3061\\u3070\", \"en\": \"Chiba\"}, \"\\u57fc\\u7389\": {\"yomi\": \"\\u3055\\u3044\\u305f\\u307e\", \"en\": \"Saitama\"}}\n" ] } ], "source": [ "print(json.dumps(D))" ] }, { "cell_type": "code", "execution_count": 32, "id": "9b0a8bd3-7817-409d-9af0-4e9b51299c86", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"東京\": {\"yomi\": \"とうきょう\", \"en\": \"Tokyo\"}, \"神奈川\": {\"yomi\": \"かながわ\", \"en\": \"Kanagawa\"}, \"千葉\": {\"yomi\": \"ちば\", \"en\": \"Chiba\"}, \"埼玉\": {\"yomi\": \"さいたま\", \"en\": \"Saitama\"}}\n" ] } ], "source": [ "print(json.dumps(D, ensure_ascii=False))" ] }, { "cell_type": "code", "execution_count": 30, "id": "615b9f8a-1001-4c67-957d-e4c4d44bad82", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"東京\": {\n", " \"yomi\": \"とうきょう\",\n", " \"en\": \"Tokyo\"\n", " },\n", " \"神奈川\": {\n", " \"yomi\": \"かながわ\",\n", " \"en\": \"Kanagawa\"\n", " },\n", " \"千葉\": {\n", " \"yomi\": \"ちば\",\n", " \"en\": \"Chiba\"\n", " },\n", " \"埼玉\": {\n", " \"yomi\": \"さいたま\",\n", " \"en\": \"Saitama\"\n", " }\n", "}\n" ] } ], "source": [ "print(json.dumps(D, ensure_ascii=False, indent=2))" ] }, { "cell_type": "code", "execution_count": 33, "id": "c4aec11f-c647-4d02-84b2-549f8c09bce9", "metadata": {}, "outputs": [], "source": [ "with open('sample.json', 'w') as fo:\n", " json.dump(D, fo)" ] }, { "cell_type": "code", "execution_count": 34, "id": "fed2eb7d-1e84-4816-b7b8-282f5a620c5c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"\\u6771\\u4eac\": {\"yomi\": \"\\u3068\\u3046\\u304d\\u3087\\u3046\", \"en\": \"Tokyo\"}, \"\\u795e\\u5948\\u5ddd\": {\"yomi\": \"\\u304b\\u306a\\u304c\\u308f\", \"en\": \"Kanagawa\"}, \"\\u5343\\u8449\": {\"yomi\": \"\\u3061\\u3070\", \"en\": \"Chiba\"}, \"\\u57fc\\u7389\": {\"yomi\": \"\\u3055\\u3044\\u305f\\u307e\", \"en\": \"Saitama\"}}" ] } ], "source": [ "!cat sample.json" ] }, { "cell_type": "code", "execution_count": 35, "id": "77db1b87-e8f3-471e-a0e1-6d7767b43cb6", "metadata": {}, "outputs": [], "source": [ "del D" ] }, { "cell_type": "code", "execution_count": 36, "id": "68689e45-3f68-421a-bf9a-17c82e19b1c4", "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "NameError", "evalue": "name 'D' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mD\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'D' is not defined" ] } ], "source": [ "D" ] }, { "cell_type": "code", "execution_count": 37, "id": "61786554-2767-4f22-9ffb-2500493751f0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'東京': {'yomi': 'とうきょう', 'en': 'Tokyo'},\n", " '神奈川': {'yomi': 'かながわ', 'en': 'Kanagawa'},\n", " '千葉': {'yomi': 'ちば', 'en': 'Chiba'},\n", " '埼玉': {'yomi': 'さいたま', 'en': 'Saitama'}}" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open('sample.json') as fi:\n", " D = json.load(fi)\n", "D" ] }, { "cell_type": "markdown", "id": "49ee73b2-f0e4-4cc6-97a3-fd9461c2a0bb", "metadata": {}, "source": [ "### [pickle --- Python オブジェクトの直列化](https://docs.python.org/ja/3/library/pickle.html)" ] }, { "cell_type": "code", "execution_count": 38, "id": "43e62548-646f-4d80-8f2f-8c1d754fe40d", "metadata": {}, "outputs": [], "source": [ "import pickle" ] }, { "cell_type": "code", "execution_count": 39, "id": "bc48f078-a950-492e-857c-59514c1bca80", "metadata": {}, "outputs": [], "source": [ "D = {\n", " '東京': {'yomi': 'とうきょう', 'en': 'Tokyo'},\n", " '神奈川': {'yomi': 'かながわ', 'en': 'Kanagawa'},\n", " '千葉': {'yomi': 'ちば', 'en': 'Chiba'},\n", " '埼玉': {'yomi': 'さいたま', 'en': 'Saitama'}\n", "}" ] }, { "cell_type": "code", "execution_count": 41, "id": "15a725d9-262a-408d-bfa8-f9fca778c198", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'\\x80\\x04\\x95\\xb2\\x00\\x00\\x00\\x00\\x00\\x00\\x00}\\x94(\\x8c\\x06\\xe6\\x9d\\xb1\\xe4\\xba\\xac\\x94}\\x94(\\x8c\\x04yomi\\x94\\x8c\\x0f\\xe3\\x81\\xa8\\xe3\\x81\\x86\\xe3\\x81\\x8d\\xe3\\x82\\x87\\xe3\\x81\\x86\\x94\\x8c\\x02en\\x94\\x8c\\x05Tokyo\\x94u\\x8c\\t\\xe7\\xa5\\x9e\\xe5\\xa5\\x88\\xe5\\xb7\\x9d\\x94}\\x94(h\\x03\\x8c\\x0c\\xe3\\x81\\x8b\\xe3\\x81\\xaa\\xe3\\x81\\x8c\\xe3\\x82\\x8f\\x94h\\x05\\x8c\\x08Kanagawa\\x94u\\x8c\\x06\\xe5\\x8d\\x83\\xe8\\x91\\x89\\x94}\\x94(h\\x03\\x8c\\x06\\xe3\\x81\\xa1\\xe3\\x81\\xb0\\x94h\\x05\\x8c\\x05Chiba\\x94u\\x8c\\x06\\xe5\\x9f\\xbc\\xe7\\x8e\\x89\\x94}\\x94(h\\x03\\x8c\\x0c\\xe3\\x81\\x95\\xe3\\x81\\x84\\xe3\\x81\\x9f\\xe3\\x81\\xbe\\x94h\\x05\\x8c\\x07Saitama\\x94uu.'" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pickle.dumps(D)" ] }, { "cell_type": "code", "execution_count": 44, "id": "aafbc150-7222-4c38-a312-b6f1bfa7a2d9", "metadata": {}, "outputs": [], "source": [ "with open('sample.pkl', 'wb') as fo:\n", " pickle.dump(D, fo)" ] }, { "cell_type": "code", "execution_count": 45, "id": "66c29c85-4a4f-4a2e-86f0-09aff88e9fa1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'東京': {'yomi': 'とうきょう', 'en': 'Tokyo'},\n", " '神奈川': {'yomi': 'かながわ', 'en': 'Kanagawa'},\n", " '千葉': {'yomi': 'ちば', 'en': 'Chiba'},\n", " '埼玉': {'yomi': 'さいたま', 'en': 'Saitama'}}" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open('sample.pkl', 'rb') as fi:\n", " D = pickle.load(fi)\n", "D" ] }, { "cell_type": "markdown", "id": "dd2806b6-ce6a-4679-aea6-72915fd1553c", "metadata": {}, "source": [ "### [struct --- バイト列をパックされたバイナリデータとして解釈する](https://docs.python.org/ja/3/library/struct.html)" ] }, { "cell_type": "code", "execution_count": null, "id": "202a3730-ba9e-4556-b00f-e69e94bb1c3c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "26e11f98-6932-4570-9ec1-392da76837a1", "metadata": {}, "source": [ "### [pprint --- データ出力の整然化](https://docs.python.org/ja/3/library/pprint.html)" ] }, { "cell_type": "code", "execution_count": 46, "id": "25e24115-6ddb-46b3-9e5a-d28a34344563", "metadata": {}, "outputs": [], "source": [ "import pprint" ] }, { "cell_type": "code", "execution_count": 47, "id": "4bffe709-ac57-4c6e-857c-bf24aededde9", "metadata": {}, "outputs": [], "source": [ "D = {\n", " '東京': {'yomi': 'とうきょう', 'en': 'Tokyo'},\n", " '神奈川': {'yomi': 'かながわ', 'en': 'Kanagawa'},\n", " '千葉': {'yomi': 'ちば', 'en': 'Chiba'},\n", " '埼玉': {'yomi': 'さいたま', 'en': 'Saitama'}\n", "}" ] }, { "cell_type": "code", "execution_count": 48, "id": "065abd77-31f1-4b20-a4cc-09bdd90f4b2d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'東京': {'yomi': 'とうきょう', 'en': 'Tokyo'}, '神奈川': {'yomi': 'かながわ', 'en': 'Kanagawa'}, '千葉': {'yomi': 'ちば', 'en': 'Chiba'}, '埼玉': {'yomi': 'さいたま', 'en': 'Saitama'}}\n" ] } ], "source": [ "print(D)" ] }, { "cell_type": "code", "execution_count": 49, "id": "e9f99811-559d-4508-b88c-56e0668aa4c9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'千葉': {'en': 'Chiba', 'yomi': 'ちば'},\n", " '埼玉': {'en': 'Saitama', 'yomi': 'さいたま'},\n", " '東京': {'en': 'Tokyo', 'yomi': 'とうきょう'},\n", " '神奈川': {'en': 'Kanagawa', 'yomi': 'かながわ'}}\n" ] } ], "source": [ "pprint.pprint(D)" ] }, { "cell_type": "code", "execution_count": 51, "id": "800c2abd-7600-40cd-a34a-99eb3ec41e15", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'千葉': {'en': 'Chiba',\n", " 'yomi': 'ちば'},\n", " '埼玉': {'en': 'Saitama',\n", " 'yomi': 'さいたま'},\n", " '東京': {'en': 'Tokyo',\n", " 'yomi': 'とうきょう'},\n", " '神奈川': {'en': 'Kanagawa',\n", " 'yomi': 'かながわ'}}\n" ] } ], "source": [ "pprint.pprint(D, width=20)" ] }, { "cell_type": "markdown", "id": "28bf3f9b-0c0b-4c0f-a8a5-8659ce414564", "metadata": {}, "source": [ "### [os.path --- 共通のパス名操作](https://docs.python.org/ja/3/library/os.path.html)" ] }, { "cell_type": "markdown", "id": "7e6329ea-db9f-4f8d-bb71-cfc24b297f20", "metadata": {}, "source": [ "### [glob --- Unix 形式のパス名のパターン展開](https://docs.python.org/ja/3/library/glob.html)" ] }, { "cell_type": "markdown", "id": "4f1ee74c-486f-4d3c-84af-5ff6d24772e5", "metadata": {}, "source": [ "### [pathlib --- オブジェクト指向のファイルシステムパス](https://docs.python.org/ja/3/library/pathlib.html)" ] }, { "cell_type": "markdown", "id": "aa0fbfa3-4488-42ea-a3b2-b285dd31007f", "metadata": {}, "source": [ "### [urllib.request --- URL を開くための拡張可能なライブラリ](https://docs.python.org/ja/3/library/urllib.request.html)" ] }, { "cell_type": "markdown", "id": "3c2e7c61-ec89-44b8-a08f-dd5bc77e3a69", "metadata": {}, "source": [ "### [urllib.parse --- URL を解析して構成要素にする](https://docs.python.org/ja/3/library/urllib.parse.html)" ] }, { "cell_type": "markdown", "id": "2ebfe4ed-5726-49e6-b100-6f82b2b68ef3", "metadata": {}, "source": [ "### [sys --- システムパラメータと関数](https://docs.python.org/ja/3/library/sys.html)" ] }, { "cell_type": "code", "execution_count": 50, "id": "d2a083b2-db2a-4d75-af1c-1688e000db0e", "metadata": {}, "outputs": [], "source": [ "import sys" ] }, { "cell_type": "markdown", "id": "68e54054-fa66-4aa0-aca3-e5dd83cf64a7", "metadata": {}, "source": [ "sys.argv" ] }, { "cell_type": "markdown", "id": "7ec108dc-ba44-46b2-b489-5eb1e8916eca", "metadata": {}, "source": [ "### [os --- 雑多なオペレーティングシステムインタフェース](https://docs.python.org/ja/3/library/os.html)" ] }, { "cell_type": "markdown", "id": "491c207f-224e-4ae6-b97d-778e6468794f", "metadata": {}, "source": [ "### [argparse --- コマンドラインオプション、引数、サブコマンドのパーサー](https://docs.python.org/ja/3/library/argparse.html)" ] }, { "cell_type": "markdown", "id": "94680294-60e1-4585-bed6-454daba306b3", "metadata": {}, "source": [ "### [logging --- Python 用ロギング機能](https://docs.python.org/ja/3/library/logging.html)" ] }, { "cell_type": "code", "execution_count": null, "id": "50129bb6-c06d-4fdc-be49-7e0590f95a77", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "15233c3d-1af4-4ed6-bba0-941d932640a0", "metadata": { "tags": [] }, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "c9938831-e9f7-429a-877a-52673268b6b7", "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": 5 }