{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n",
" \n",
" Open In Jupyter nbviewer\n",
" \n",
" \n",
"
\n", " [ 起始序號 : 結束序號 : 遞增(減)量 ]\n", "
\n", "\n", "- 注意索引結果的元素,不包含結束序號的那個元素;也就是說 `[start : end]` 取的是 $[start, end)$ 的區間。\n", "\n", "| Slicing 的操作 | 說明 |\n", "|----------------|----------------------------------------------|\n", "| `S[1:3]` | 取元素範圍序號 1 到 2 |\n", "| `S[1:]` | 取元素範圍序號 1 到最後一個 |\n", "| `S[:-1]` | 取元素範圍第一個到最後一個的前一個 |\n", "| `S[:]` | 取所有範圍的元素 |\n", "| `S[2::3]` | 元素範圍序號 2 到最後一個,每遞增 3 個序號取 |\n", "| `S[::2]` | 所有元素的範圍內,從頭到尾每隔一個序號取 |\n", "| `S[::-1]` | 反向取所有範圍的元素 |\n", "| `S[-2:0:-1]` | 所有範圍的元素去頭尾,反向取 |" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1]" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# List slicing 練習\n", "L = list(range(1, 10))\n", "L[:-1] + L[::-1]" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[7, 8, 9, 8, 7, 6, 5, 4, 3, 2]" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# List slicing 練習\n", "L[6:] + L[-2:0:-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### § 成員操作 Membership Operations\n", "| 成員的操作 | 說明 |\n", "|------------------------|------------------------------------------------------------------|\n", "| `x in S` | S 的成員裡有 x(只要值相同就算),結果為 `True` 或 `False` |\n", "| `x not in S` | S 的成員裡沒有 x ,結果為 `True` 或 `False` |\n", "| `for x in S:` | (迴圈)列舉 S 裡的成員到 x |\n" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 判斷是否存在成員\n", "L = list(range(10))\n", "(9 in L) and (10 not in L)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### § 卸載操作 Unpacking Operations\n", "當序列容器或可迭代(iterable)物件在等號(`=`, assignment)右邊,等號左邊如果有對應元素個數的變數,容器的元素內容會自動分派卸載。\n", "\n", "| 自動卸載 | 結果 |\n", "|----------------------------|------------------------|\n", "| `a, b, c = [1, 2, 3]` | a = 1, b = 2, c = 3 |\n", "| `a, b, c = (1, 2, 3)` | a = 1, b = 2, c = 3 |\n", "| `a, b, c = 1, 2, 3` | a = 1, b = 2, c = 3 |\n", "| `a, b, c = range(3)` | a = 0, b = 1, c = 2 |\n", "\n", "容器卸載是將每個元素拆到容器外,卸載操作通常用在卸載不同類型容器的內容到新容器,或容器內容卸載為叫用函式的參數。 一般可以列舉的、序列型態的容器、字典的 key 都用 `*` 操作,字典容器的 value 用 `**` 卸載。\n", "\n", "| 卸載操作 | 輸出 |\n", "|---------------------------------------------------------|------------------------|\n", "| `print(*range(4))` | 0 1 2 3 |\n", "| `*range(4),` | (0, 1, 2, 3) |\n", "| `[*range(4), 4]` | [0, 1, 2, 3, 4] |\n", "| `'unpack keys: {}, {}'.format(*{'a':1, 'c':3})` | 'unpack keys: a, c' |\n", "| `'unpack values: {a}, {c}'.format(**{'a':1, 'c':3})` | 'unpack values: 1, 3' |" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3 4 5\n" ] } ], "source": [ "# 用在函式的呼叫,卸載到函式的參數\n", "print(*[1], *[2], 3, *[4, 5])" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7]" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 用在將非同類型容器的內容卸載到新容器\n", "[*range(4), 4, *(5, 6, 7)]" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Coordinate System: latitude, longitude'" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 卸載 dict 的 key\n", "'Coordinate System: {}, {}'.format(*{'latitude': '37.24N', 'longitude': '-115.81W'})" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Coordinates: 37.24N, -115.81W'" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 卸載 dict 的 value\n", "'Coordinates: {latitude}, {longitude}'.format(**{'latitude': '37.24N', 'longitude': '-115.81W'})" ] } ], "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.8.8" } }, "nbformat": 4, "nbformat_minor": 2 }