Created
September 20, 2018 05:50
-
-
Save nagishin/8e6f0d13d05d2cf9efd68223dc146289 to your computer and use it in GitHub Desktop.
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 文字列補間" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### %補間" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"単純な補間" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"a:5\n", | |
"b:1.234500\n", | |
"c:moji\n" | |
] | |
} | |
], | |
"source": [ | |
"a = 5\n", | |
"b = 1.2345\n", | |
"c = \"moji\"\n", | |
"print(\"a:%d\" % a) # int:整数\n", | |
"print(\"b:%f\" % b) # float:浮動小数\n", | |
"print(\"c:%s\" % c) # string:文字列" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"複数項目を補間(タプル形式)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"a:5 b:1.234500 c:moji\n" | |
] | |
} | |
], | |
"source": [ | |
"print(\"a:%d b:%f c:%s\" % (a, b, c)) # 保管する順番にタプルに格納" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"桁数を指定して補間" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"a: 5\n", | |
"b:1.234\n", | |
"c: moji\n" | |
] | |
} | |
], | |
"source": [ | |
"print(\"a:%5d\" % a) # 5桁表示\n", | |
"print(\"b:%.3f\" % b) # 小数点以下3桁表示\n", | |
"print(\"c:%5s\" % c) # 5桁表示" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"組合せ応用例" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[ 1] 1 / 2 = 0.5000\n", | |
"[ 2] 2 / 3 = 0.6667\n", | |
"[ 3] 3 / 4 = 0.7500\n", | |
"[ 4] 4 / 5 = 0.8000\n", | |
"[ 5] 5 / 6 = 0.8333\n", | |
"[ 6] 6 / 7 = 0.8571\n", | |
"[ 7] 7 / 8 = 0.8750\n", | |
"[ 8] 8 / 9 = 0.8889\n", | |
"[ 9] 9 / 10 = 0.9000\n", | |
"[10] 10 / 11 = 0.9091\n" | |
] | |
} | |
], | |
"source": [ | |
"for i in range(1, 11):\n", | |
" print(\"[%2d] %2d / %2d = %.4f\" % (i, i, i+1, i/(i+1)))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### format関数" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"単純な補間" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"a:5\n", | |
"b:1.2345\n", | |
"c:moji\n" | |
] | |
} | |
], | |
"source": [ | |
"print(\"a:{}\".format(a))\n", | |
"print(\"b:{}\".format(b))\n", | |
"print(\"c:{}\".format(c))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"複数項目を補間" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"a:5 b:1.2345 c:moji\n" | |
] | |
} | |
], | |
"source": [ | |
"print(\"a:{} b:{} c:{}\".format(a, b, c))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"リストを補間" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 69, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"lst1: [5, 1.2345, 'moji']\n", | |
"a:5 b:1.2345 c:moji\n" | |
] | |
} | |
], | |
"source": [ | |
"lst1 = [a, b, c]\n", | |
"print(\"lst1:\", lst1)\n", | |
"print(\"a:{} b:{} c:{}\".format(*lst1)) # Listの先頭要素から順に補間" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"複数のリストを補間" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 89, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"lst2: [10, 100, 500]\n", | |
"lst1:[5, 1.2345, moji] lst2:[10, 100, 500]\n", | |
"lst1:[5, 1.2345, moji] lst2:[10, 100, 500]\n", | |
"lst1:[5, 1.2345, moji] lst2:[10, 100, 500]\n" | |
] | |
} | |
], | |
"source": [ | |
"lst2 = [10, 100, 500]\n", | |
"print(\"lst2:\", lst2)\n", | |
"# Listが連結されて順に補間\n", | |
"print(\"lst1:[{}, {}, {}] lst2:[{}, {}, {}]\".format(*lst1, *lst2))\n", | |
"# Listが連結されてindex指定して補間\n", | |
"print(\"lst1:[{0}, {1}, {2}] lst2:[{3}, {4}, {5}]\".format(*lst1, *lst2))\n", | |
"# Listとindexを指定して補間(lst1:0, lst2:1)\n", | |
"print(\"lst1:[{0[0]}, {0[1]}, {0[2]}] lst2:[{1[0]}, {1[1]}, {1[2]}]\".format(lst1, lst2))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"辞書を補間" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 84, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"dic1: {'a': 5, 'b': 1.2345, 'c': 'moji'}\n", | |
"keys:[a, b, c]\n", | |
"vals:[5, 1.2345, moji]\n" | |
] | |
} | |
], | |
"source": [ | |
"dic1 = {\"a\":a, \"b\":b, \"c\":c}\n", | |
"print(\"dic1:\", dic1)\n", | |
"print(\"keys:[{}, {}, {}]\".format(*dic1)) # 辞書のkeysリストを順に補間\n", | |
"print(\"vals:[{a}, {b}, {c}]\".format(**dic1)) # 辞書のkeyを指定してvalueを補間" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"複数の辞書を補間" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 93, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"dic2: {'d': 10, 'e': 100, 'f': 500}\n", | |
"keys1:[a, b, c] keys2:[d, e, f]\n", | |
"keys1:[a, a, a] keys2:[b, b, b]\n", | |
"vals1:[5, 1.2345, moji] vals2:[10, 100, 500]\n", | |
"vals1:[5, 1.2345, moji] vals2:[10, 100, 500]\n" | |
] | |
} | |
], | |
"source": [ | |
"dic2 = {\"d\":10, \"e\":100, \"f\":500}\n", | |
"print(\"dic2:\", dic2)\n", | |
"# 辞書のkeysリストが連結されて順に補間\n", | |
"print(\"keys1:[{}, {}, {}] keys2:[{}, {}, {}]\".format(*dic1, *dic2))\n", | |
"# 辞書のkeysリストを指定して順に補間(dic1.keys:0, dic2.keys:1)\n", | |
"print(\"keys1:[{0}, {0}, {0}] keys2:[{1}, {1}, {1}]\".format(*dic1, *dic2))\n", | |
"# 辞書のkeyを指定してvalueを補間\n", | |
"print(\"vals1:[{a}, {b}, {c}] vals2:[{d}, {e}, {f}]\".format(**dic1, **dic2))\n", | |
"# 辞書とkeyを指定して補間(dic1.keys:0, dic2.keys:1)\n", | |
"print(\"vals1:[{0[a]}, {0[b]}, {0[c]}] vals2:[{1[d]}, {1[e]}, {1[f]}]\".format(dic1, dic2))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"桁合わせ(左寄せ, 右寄せ, センタリング)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 104, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1234567890\n", | |
"moji \n", | |
" moji \n", | |
" moji\n", | |
"moji******\n", | |
"$$$moji$$$\n", | |
"??????moji\n" | |
] | |
} | |
], | |
"source": [ | |
"str1 = \"1234567890\"\n", | |
"str2 = \"moji\"\n", | |
"print(str1)\n", | |
"print(\"{:<10}\".format(str2)) # 左寄せ\n", | |
"print(\"{:^10}\".format(str2)) # センタリング\n", | |
"print(\"{:>10}\".format(str2)) # 右寄せ\n", | |
"\n", | |
"print(\"{:*<10}\".format(str2)) # 左寄せ(*埋め)\n", | |
"print(\"{:$^10}\".format(str2)) # センタリング($埋め)\n", | |
"print(\"{:?>10}\".format(str2)) # 右寄せ(?埋め)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"数値の各種表示加工" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 134, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"123456789012345\n", | |
"00000123456.789\n", | |
" 123,456.789\n", | |
"+123,456.789 \n", | |
" 123456.7890\n", | |
" 1.235e+05\n", | |
" 1.2346e+05\n", | |
" 65.10%\n" | |
] | |
} | |
], | |
"source": [ | |
"str1 = \"123456789012345\"\n", | |
"num = 123456.789\n", | |
"print(str1)\n", | |
"print(\"{:>015}\".format(num)) # 15桁, ゼロ埋め, 右寄せ\n", | |
"print(\"{:>15,}\".format(num)) # 15桁, カンマ区切り, 右寄せ\n", | |
"print(\"{:<+15,}\".format(num)) # 15桁, 符号表示, カンマ区切り, 左寄せ\n", | |
"print(\"{:>15.4f}\".format(num)) # 15桁, 小数点以下4桁数, 右寄せ\n", | |
"print(\"{:>15.4g}\".format(num)) # 15桁, 有効桁:4桁, 右寄せ\n", | |
"print(\"{:>15.4e}\".format(num)) # 15桁, 指数表記:4桁, 右寄せ\n", | |
"print(\"{:>15.2%}\".format(0.651)) # 15桁, パーセント表記:小数2桁, 右寄せ" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.6.2" | |
}, | |
"toc": { | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment