{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 流程控制 - Control Flow" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "3pFRlUbn--qW" }, "source": [ "本章節內容大綱\n", "* [敘述 Statement](#敘述)\n", "* [if-else](#if-else)\n", "* [for-loop](#for-loop)\n", " * [range迭代](#range迭代)\n", " * [container迭代](#container迭代)\n", " * [enumerate迭代](#enumerate迭代)\n", " * [zip迭代](#zip迭代)\n", " * [nested loop](#nested-loop)\n", "* [while-loop](#while-loop)\n", "* [Break](#Break)\n", "* [Continue](#Continue)\n", "* [Pass vs Continue](#Pass-vs-Continue)\n", "* [生成式](#生成式)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "nXs6LqVZ--qX" }, "source": [ "## 敘述" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "1UuZBsXR--qY" }, "outputs": [], "source": [ "A = 5\n", "\n", "statement1 = A > 0\n", "\n", "print(statement1)\n", "\n", "statement2 = A > 0 and A < 100\n", "\n", "print(statement2)\n", "\n", "statement3 = type(A) is float\n", "\n", "print(statement3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "fHltwgfy--qd" }, "outputs": [], "source": [ "D = [1, 2, 3, None, 5]\n", "\n", "statement1 = 1 in D\n", "\n", "print(statement1)\n", "\n", "statement2 = 4 in D\n", "\n", "print(statement2)\n", "\n", "statement3 = 4 not in D\n", "\n", "print(statement3)\n", "\n", "statement4 = D[3] is not None\n", "\n", "print(statement4)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "BjhddFBT--qg" }, "source": [ "## if-else" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "sMtAhWQU--qh", "outputId": "4d0ce332-5260-45b3-a660-15ad5883db3f" }, "outputs": [], "source": [ "''' if-else 的使用格式:\n", "if statement1 :\n", " (do something)\n", "elif statement2 :\n", " (do something)\n", "else:\n", " (do something)\n", "\n", "如果你曾經寫過像是 C 等其他語言,注意 python 的 statement 不需要在外面加括號,但是在敘述後要加上冒號 (:)。\n", "至於在該 statement 內的程式碼都必須縮排 (可以使用 tab)。\n", "'''\n", "\n", "n = int(input(f'Please enter an integer: '))\n", "\n", "if n > 0:\n", " print(f'It is a positive number.')\n", "elif n < 0:\n", " print(f'It is a negative number.')\n", "else:\n", " print(f'It is zero')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "ayNfUlCr--qn", "outputId": "17dfa77d-09dd-4636-81df-1e3162d5555d" }, "outputs": [], "source": [ "h = float(input(f'Please enter your height(cm): '))\n", "w = float(input(f'Please enter your weight(kg): '))\n", "\n", "BMI = w / ((h / 100)**2)\n", "\n", "# elif 可以有很多個,但是最開始必須為 if,而 else 不一定為必要\n", "\n", "if BMI > 35:\n", " print(f'Your BMI is {BMI:.1f}, you are extremely fat!')\n", "elif BMI > 30 and BMI <= 35:\n", " print(f'Your BMI is {BMI:.1f}, you are fat!')\n", "elif BMI > 27 and BMI <= 30:\n", " print(f'Your BMI is {BMI:.1f}, you are slightly fat!')\n", "elif BMI > 24 and BMI <= 27:\n", " print(f'Your BMI is {BMI:.1f}, you are overweight!')\n", "elif BMI > 18.5 and BMI <= 24:\n", " print(f'Your BMI is {BMI:.1f}, you have a normal weight.')\n", "else:\n", " print(f'Your BMI is {BMI:.1f}, you are too light!')" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "5dks77FJ--qq" }, "source": [ "## for-loop\n", "\n", "若有需要重複執行的程式,即可交給for-loop!\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Yxy_Yugx--q6" }, "source": [ "### container迭代" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "ZrpH0W3Z--q7" }, "outputs": [], "source": [ "# list 的迭代\n", "L = ['a', 'b', 'c', 'd', 'e']\n", "\n", "for element in L:\n", " print(element, end=\" \")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for element in L[:3]:\n", " print(element, end=\" \")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for element in reversed(L):\n", " print(element, end=\" \")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "4_enM6AL--q-" }, "outputs": [], "source": [ "T = ('a', 'b', 'c', 'd', 'e')\n", "\n", "for element in T:\n", " print(element, end=\" \")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for element in T[:3]:\n", " print(element, end=\" \")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for element in reversed(T):\n", " print(element, end=\" \")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "jXIfEJrb--rB" }, "outputs": [], "source": [ "S = {'a', 'b', 'c', 'd', 'e'}\n", "\n", "# Set 雖然也可以迭代,但其無序性讓迭代的物件無法預測,故也無逆序功能\n", "for element in S:\n", " print(element, end=\" \")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "-0DJlIDr--rD" }, "outputs": [], "source": [ "D = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}\n", "for key in D.keys():\n", " print(key, end=\" \")\n", "print()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for value in D.values():\n", " print(value, end=\" \")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for key, value in D.items():\n", " print(f'({key}, {value})', end=\" \")" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "XTkfxorO--rG" }, "source": [ "### enumerate迭代" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "zJsismVr--rH" }, "outputs": [], "source": [ "L = ['a', 'b', 'c', 'd', 'e']\n", "for index, element in enumerate(L):\n", " print(f'({index}, {element})', end=\" \")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "T = ['a', 'b', 'c', 'd', 'e']\n", "for index, element in enumerate(T):\n", " print(f'({index}, {element})', end=\" \")" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "gwmBGyff--qr" }, "source": [ "### range迭代" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# for (int i = 0; i < 10; i += 1)\n", "\n", "for i in range(10):\n", " print(i, end=\" \")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# for (int i = -3; i < 10; i += 1)\n", "\n", "for i in range(-3, 10):\n", " print(i, end=\" \")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# for (int i = -3; i < 10; i += 2)\n", "\n", "for i in range(-3, 10, 2):\n", " print(i, end=\" \")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# for (int i = -1; i > -10; i -= 2)\n", "\n", "for i in range(-1, -10, -2):\n", " print(i, end=\" \")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "GlG3X-xT--rK" }, "outputs": [], "source": [ "# 我們偶爾也會使用 range(len()) 來迭代 list、set 等結構,此行為比較接近其他語言 (例如 C、Java ) 的迴圈行為\n", "# for (int i = 0; i < L.size(); i += 1)\n", "\n", "L = ['a', 'b', 'c', 'd', 'e']\n", "\n", "for index, value in enumerate(L):\n", " print(index, value)\n", "\n", "for i in range(len(L)):\n", " print(i, L[i])" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "eRuy6UHY--rQ" }, "source": [ "## nested loop" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 多層迴圈的使用必須謹慎,內層迴圈完整迭代完一次,外層才會跳下一個迭代。\n", "# 縮排需要特別注意 !\n", "'''\n", "4*4 的乘法表\n", "\n", "i j\n", "1 * 1 = 1\n", "1 * 2 = 2\n", "1 * 3 = 3\n", "1 * 4 = 4\n", "2 * 1 = 2\n", "2 * 2 = 4\n", "...\n", "4 * 4 = 16\n", "'''\n", "\n", "i = 1\n", "for j in range(1, 5):\n", " print(f'i = {i}, j = {j}, i*j = {i*j}')\n", "\n", "i = 2\n", "for j in range(1, 5):\n", " print(f'i = {i}, j = {j}, i*j = {i*j}')\n", "\n", "i = 3\n", "for j in range(1, 5):\n", " print(f'i = {i}, j = {j}, i*j = {i*j}')\n", "\n", "i = 4\n", "for j in range(1, 5):\n", " print(f'i = {i}, j = {j}, i*j = {i*j}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 此例子中,當 i 最初為 1 時,j 會完整地從 1 迭代到 4 結束, 接著 i 才會到下一個 2。\n", "for i in range(1, 5):\n", " for j in range(1, 5):\n", " print(f'i = {i}, j = {j}, i*j = {i*j}')\n", " print('---------------------')" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "8koDIBDC--rM" }, "source": [ "### zip迭代" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "TGf0ySku--rM" }, "outputs": [], "source": [ "# 除了常規的四大資料結構迭代,我們也可以使用 zip 將任意不同的可迭代結構打包起來,一起做迭代。\n", "# zip 會將兩結構中同一位置的物件擺在同一個迭代順位,例如下例中 'Apple' 即會跟 40 擺在同一個順位。\n", "# 使用 zip 的前提是,兩結構的長度必須一樣 ! (可使用 len() 先行確認 )\n", "\n", "L1 = ['Apple', 'Banana', 'Carrot', 'Donut']\n", "L2 = [40, 15, 25, 35]\n", "\n", "for product, price in zip(L1, L2):\n", " print(f'({product}, {price})', end=\" \")" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "LEpLkFRG--rW" }, "source": [ "## while-loop" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "xvURzBGg--rW" }, "outputs": [], "source": [ "'''\n", "while迴圈執行的原則為,只要 statement 還是對的,迴圈就會繼續重複,直到敘述錯誤停止。下面是它的語法\n", "\n", "while statement:\n", " do something\n", "'''" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "uqfLwJ0Z--rZ" }, "outputs": [], "source": [ "x = 0\n", "\n", "# 在此範例中,x 一開始為 0,所以敘述為真\n", "# 迴圈內每執行一次, x 的值即增多 1\n", "while x < 5:\n", " x = x + 1\n", " print(x)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "vYoVd9rq--rb" }, "source": [ "## Break" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 使用 break 也可以在迴圈中直接離開迴圈\n", "\n", "x = 0\n", "while x < 5:\n", " x = x + 1\n", " print(x)\n", " break" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 在 for 迴圈中使用 break 跳出迴圈\n", "\n", "for x in range(5):\n", " print(x)\n", " break" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "ELcBxWUl--rc" }, "outputs": [], "source": [ "# 若我們希望迴圈在發生某個情況時停止,但不確定那件事會在甚麼時候發生,可以使用無窮迴圈,也就是 while True。\n", "# 並在真正發生的時候使用 break 跳出迴圈。\n", "\n", "# 假設我們想要使用者輸入一個 \"三位數字\",但由於無法預測使用者的行為,不知道他何時才會真的輸入三位數,這種時候就可使用 while True\n", "# (持續做 input()),若發現 input() 符合三位數,再行 break 跳出迴圈。\n", "\n", "while True:\n", " value = input('Please Enter a number with 3 digits: ')\n", " if len(value) == 3 and value.isdigit():\n", " break\n", "\n", "print(f'You entered {value}')" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "RbsG5zfV--rf" }, "source": [ "## Continue" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "feMRKbqP--rg" }, "outputs": [], "source": [ "# 在某些情況,我們想跳過該次迭代,但又不想結束迴圈,這時我們可以使用 continue。\n", "\n", "# 範例:我們想要計算使用者輸入的 \"數字\" 總和,輸入次數不限,只要是數字就進行累加,若非數字則跳過,直到使用者輸入'q'結束程式\n", "# 必須涵蓋三個部分:\n", "# 1. 輸入 'q' 即結束迴圈 (使用 break)\n", "# 2. 輸入不是數字時跳過累加 (使用 continue)\n", "# 3. 其他情況 (輸入為數字),累加\n", "\n", "Sum = 0\n", "while True:\n", "\n", " value = input('Please Enter a number: ') # value: string\n", "\n", " if value == 'q':\n", " print('Quitting loop')\n", " break\n", " elif not value.isdigit(): # .isdigit():若該字串皆是數字,返回True; 若非數字,返回False\n", " print(f'Not a number, ignored')\n", " continue\n", "\n", " Sum += int(value)\n", "\n", "print(f'The sum of numbers you entered: {Sum}')" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "D_DZwAQY--ri" }, "source": [ "## Pass vs Continue" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "executionInfo": { "elapsed": 1425, "status": "ok", "timestamp": 1595060027990, "user": { "displayName": "張尹貞", "photoUrl": "", "userId": "01946702787608190322" }, "user_tz": -480 }, "id": "_YC0UjMf--rj" }, "outputs": [], "source": [ "for i in range(3):\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "VIR-qvh3--rm" }, "outputs": [], "source": [ "# pass無作用,程式會繼續執行\n", "for i in range(3):\n", " pass\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "1I30SU2n--rp" }, "outputs": [], "source": [ "# continue會直接跳過此次迭代\n", "for i in range(3):\n", " continue\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "F0I2tqnL--rr" }, "source": [ "## 生成式" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "jL3Sn0sE--rs", "outputId": "ac6456e6-6b6e-4896-b0cf-06b5e5bf62f2" }, "outputs": [], "source": [ "# L = list(range(6))\n", "# print(L)\n", "\n", "L = []\n", "\n", "for i in range(6):\n", " L.append(i)\n", "print(L)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "NeIv0VqS--ru", "outputId": "3d60e00b-528e-4354-92e4-b9dd9d586348" }, "outputs": [], "source": [ "# list 生成式的格式為 :\n", "# [運算式 for 項目 in 可迭代項目 (if 條件式)]\n", "# 注意運算式可為任意物件,例如 30, \"HI\", [i, i+1, i+2] ... 等\n", "\n", "L = [i+1 for i in range(6)]\n", "# i = 0, 1, 2, 3, 4, 5\n", "# L = [1, 2, 3, 4, 5, 6]\n", "print(L)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "mmVYrdMT--rw", "outputId": "286a0e5e-7a6f-4f41-c4f2-0d77e7718f42" }, "outputs": [], "source": [ "# List 的生成式 + 條件控制\n", "L = [i for i in range(6) if i % 2 == 0]\n", "# i = 0, 2, 4\n", "# L =[0, 2, 4]\n", "print(L)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "HU2EoT7V--r0", "outputId": "14113009-a603-4b2a-cbdc-bfd094b33bae" }, "outputs": [], "source": [ "# Dictionary 的生成式,運算式須為 key : value\n", "D = {i+1: (i+1)**2 for i in range(6)}\n", "print(D)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "8EAej2AZ--r2", "outputId": "54ee338d-929a-45ca-d435-71b02ba2370f" }, "outputs": [], "source": [ "# Set 的生成式\n", "S = {i for i in range(6)}\n", "print(S)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "N6xjSRc6--r5" }, "outputs": [], "source": [ "# Tuple 沒有生成式 !" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "\n", "---\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Quiz" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "lRN4RLsX--sQ" }, "outputs": [], "source": [ "# quiz1: 請使用 control flow 結合 for迴圈,計算此成績列的 GPA。\n", "\n", "# 成績與 GP 對照表\n", "# 90 <= score : 4.3\n", "# 85 <= score < 90 : 4.0\n", "# 80 <= score < 85 : 3.7\n", "# 77 <= score < 80 : 3.3\n", "# 73 <= score < 77 : 3.0\n", "# 70 <= score < 73 : 2.7\n", "# 67 <= score < 70 : 2.3\n", "# 63 <= score < 67 : 2.0\n", "# 60 <= score < 63 : 1.7\n", "# 50 <= score < 60 : 1.0\n", "# score < 50 : 0\n", "\n", "# 每筆資料為 (科目成績, 科目學分數)\n", "transcript = [(50, 1), (78, 2), (96, 2), (80, 3), (81, 2), (56, 3), (73, 3), (80, 3), (99, 2), (95, 1),\n", " (87, 3), (76, 3), (77, 1), (81, 3), (89, 2), (53, 3), (59, 2), (68, 2), (54, 3), (58, 3)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "t10uyhzX--sT" }, "outputs": [], "source": [ "# quiz2:下面是一個資料集的影像名稱,每張影像除了有六碼編號之外,還有它屬於的類別 (01 ~ 04)\n", "\n", "data = ['IMG035833_02.jpeg',\n", " 'IMG069933_03.jpeg',\n", " 'IMG001572_02.jpeg',\n", " 'IMG025945_02.jpeg',\n", " 'IMG024850_01.jpeg',\n", " 'IMG086288_03.jpeg',\n", " 'IMG069932_04.jpeg',\n", " 'IMG032322_04.jpeg',\n", " 'IMG049740_03.jpeg',\n", " 'IMG013506_03.jpeg',\n", " 'IMG001813_01.jpeg',\n", " 'IMG014431_04.jpeg',\n", " 'IMG026580_04.jpeg',\n", " 'IMG028975_04.jpeg',\n", " 'IMG079073_04.jpeg',\n", " 'IMG049077_02.jpeg',\n", " 'IMG033090_04.jpeg',\n", " 'IMG040904_04.jpeg',\n", " 'IMG065895_03.jpeg',\n", " 'IMG012382_01.jpeg',\n", " 'IMG028850_03.jpeg',\n", " 'IMG068507_01.jpeg',\n", " 'IMG078936_04.jpeg',\n", " 'IMG003145_04.jpeg',\n", " 'IMG056011_02.jpeg',\n", " 'IMG015516_01.jpeg',\n", " 'IMG077548_02.jpeg',\n", " 'IMG040693_01.jpeg',\n", " 'IMG015801_02.jpeg',\n", " 'IMG066898_02.jpeg',\n", " 'IMG039423_04.jpeg',\n", " 'IMG085263_03.jpeg',\n", " 'IMG068941_04.jpeg',\n", " 'IMG028542_03.jpeg',\n", " 'IMG016187_01.jpeg',\n", " 'IMG046760_02.jpeg',\n", " 'IMG083860_03.jpeg',\n", " 'IMG012974_03.jpeg',\n", " 'IMG094728_04.jpeg',\n", " 'IMG023535_01.jpeg',\n", " 'IMG046037_04.jpeg',\n", " 'IMG084306_02.jpeg',\n", " 'IMG008328_01.jpeg',\n", " 'IMG097630_04.jpeg',\n", " 'IMG046427_02.jpeg',\n", " 'IMG098467_03.jpeg',\n", " 'IMG078326_02.jpeg',\n", " 'IMG036626_03.jpeg',\n", " 'IMG060321_04.jpeg',\n", " 'IMG082753_01.jpeg',\n", " 'IMG066053_02.jpeg',\n", " 'IMG082360_04.jpeg',\n", " 'IMG082000_02.jpeg',\n", " 'IMG098735_04.jpeg',\n", " 'IMG028116_03.jpeg',\n", " 'IMG018454_03.jpeg',\n", " 'IMG053744_03.jpeg',\n", " 'IMG010996_04.jpeg',\n", " 'IMG062445_03.jpeg',\n", " 'IMG040778_03.jpeg',\n", " 'IMG034566_01.jpeg',\n", " 'IMG095526_04.jpeg',\n", " 'IMG010351_03.jpeg',\n", " 'IMG085847_04.jpeg',\n", " 'IMG013204_03.jpeg',\n", " 'IMG060903_03.jpeg',\n", " 'IMG043702_03.jpeg',\n", " 'IMG020717_01.jpeg',\n", " 'IMG026048_02.jpeg',\n", " 'IMG068022_03.jpeg']\n", "\n", "\n", "# 觀察上面檔名的規律,並宣告四個 list,設計程式將上述影像檔名,把他們的編號放置到四個對應的類別 list 中。\n", "\n", "# 做為參考,在執行完你的程式後,四個 list 會存入的東西分別為:\n", "# category1_id = ['024850', '001813', '012382', '068507' .... ]\n", "# category2_id = ['035833', '001572', '025945', '049077' .... ]\n", "# category3_id = ['069933', '086288', '049740', '013506' .... ]\n", "# category4_id = ['069932', '032322', '014431', '026580' .... ]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "F7Txkeku--sW" }, "outputs": [], "source": [ "# quiz3:\n", "\n", "# 請使用 list comprehension 建構出一個二維的 list,內容如下\n", "\n", "# [[10, 11, 12, 13, 14, 16, 17, 18, 19],\n", "# [20, 21, 22, 23, 24, 26, 27, 28, 29],\n", "# [30, 31, 32, 33, 34, 36, 37, 38, 39],\n", "# [40, 41, 42, 43, 44, 46, 47, 48, 49],\n", "# [60, 61, 62, 63, 64, 66, 67, 68, 69],\n", "# [70, 71, 72, 73, 74, 76, 77, 78, 79],\n", "# [80, 81, 82, 83, 84, 86, 87, 88, 89],\n", "# [90, 91, 92, 93, 94, 96, 97, 98, 99]]" ] } ], "metadata": { "colab": { "name": "ControlFlow_v2.ipynb", "provenance": [] }, "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.7.6" }, "vscode": { "interpreter": { "hash": "f2c5ff1dcfb7037f8bc8783b5d7cd749bee6b69d77e75d46cb9007d97777ae49" } } }, "nbformat": 4, "nbformat_minor": 4 }