{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "![](images/python_with_Birds.gif)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(ch3-2)=\n", "# List (ลิสต์)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ในบทที่แล้ว เราได้เรียนรู้เกี่ยวกับสตริง (str) ซึ่งเป็นออบเจกต์ประเภทลำดับ (Sequential datatype) ที่เก็บเฉพาะข้อมูลที่เป็นตัวอักษร และทูเพิล (tuple) ซึ่งเป็นออบเจกต์ทั้งประเภท Collections (Container datatype) ที่สามารถเก็บข้อมูลหลายค่าหลายชนิดรวมกันเป็นชุดเดียวกัน และข้อมูลที่เก็บมีการจัดเรียงลำดับโดยใช้เลขดัชนี กันไปแล้ว\n", "\n", "ในบทนี้จะอธิบายเกี่ยวกับออบเจกต์ที่เป็นทั้งประเภท Collections และประเภทลำดับอีกตัวหนึ่ง นั่นคือ **Lists** ซึ่งเป็นข้อมูลเชิงโครงสร้างที่ใช้มากที่สุดในภาษาไพธอน เนื่องจากสามารถเพิ่ม/ลดและแก้ไขข้อมูลได้ (Mutable data structure) ต่างกับ Tuples ซึ่งไม่สามารถแก้เปลี่ยนข้อมูลได้ (Immutable data structure) เราจะทำความรู้จักกับ Lists ว่าคืออะไร เรียนรู้วิธีการประกาศและการใช้งาน Lists เรียนรู้การใช้งานเมธอดและฟังก์ชันต่างๆ ของ Lists\n", "และเรียนรู้ความแตกต่างระหว่างการก๊อปปี้ (Copy) และการโคลน (Clone) ของ Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ref:\n", "* https://docs.python.org/3/tutorial/introduction.html#lists\n", "* https://realpython.com/python-lists-tuples/\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List (ลิสต์) คือ" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\qquad$ ข้อมูลประเภท **List (ลิสต์) ถือเป็นจุดเด่นหนึ่งของภาษาไพธอน** **List** เป็นประเภทข้อมูลที่สามารถจัดเก็บออบเจกต์ประเภทต่างๆ ให้เป็นชุด (Heterogeneous container) และมีลำดับ (Sequence Data Type) กล่าวคือ มันสามารถจัดเก็บข้อมูลได้หลายค่าในตัวแปรเดียว โดยข้อมูลที่เก็บจะเป็นชนิดเดียวกันหรือต่างชนิดกัน ก็ได้ (เช่น เก็บทั้งสตริง (str), จำนวนเต็ม (int) และ จำนวนจริง (float) หรือแม้แต่ Tuple และ List รวมกันเป็นข้อมูลชุดเดียวกัน) และสามารถเข้าถึงข้อมูลที่เก็บด้วย**เลขดัชนี (Index)** \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## การสร้างลิสต์และเลขดัชนี (Index)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "$\\qquad$ List ในภาษา Python จะคล้ายกับอาเรย์ (array) ในภาษา C หรืออาเรย์/เวกเตอร์ (vevtor)/ลิสต์ (list) ในภาษา C++ แต่ในภาษา C/C++ ข้อมูลที่เก็บต้องเป็นชนิดเดียวกันทั้งหมด เช่น [1, 2, 3, 4] แต่ List ในภาษา Python เก็บข้อมูลต่างชนิดกันได้ เช่น [1, 'a', [1, 2], 'string'] ทำให้ List ในภาษาไพธอนมีความยืดหยุ่นมากกว่า\n", "\n", "มาดูการประกาศและการใช้งาน List ในเบื้องต้นกัน" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\qquad$ ข้อมูลแบบ List จะถูกเขียนอยู่ในเครื่องหมายวงเล็บเหลี่ยม **[ ]** (square brackets) (หรือจะเรียกว่า 'วงเล็บก้ามปู')\n", "และคั่นสมาชิกแต่ละตัวด้วยเครื่องหมาย **,** (comma)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "_List syntax:_ \n", "\n", "```\n", "[value1, value2, value3, value4, value5, ...]\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](images/ListsIndex.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เรามาลองสร้างลิสต์ที่มีสมาชิกทุกตัวเป็นจำนวนเต็ม" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# สร้าง list ที่มีสมาชิกเป็นจำนวนเต็ม\n", "[0, 10, 20, 30, 40, 50, 60, 70, 80,90, 100]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "สร้างตัวแปรลิสต์ที่มีสมาชิกทุกตัวเป็นจำนวนเต็ม" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# สร้างตัวแปร list ที่มีสมาชิกเป็นจำนวนเต็ม\n", "numbers = [0, 10, 20, 30, 40, 50, 60, 70, 80,90, 100]\n", "numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ชนิดข้อมูลของลิสต์คือ **list** (และยังเป็นชื่อของฟังก์ชัน (list()) ที่ใช้ในการแปลงข้อมูลชนิดอื่นๆ ใช้เป็น list รายละเอียดจะอธิบายในภายหลัง)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(numbers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เรามาลองสร้างตัวแปรลิสต์อีกตัวที่สมาชิกทั้งหมดเป็นสตริง และ boolean" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['Python', 'C', 'C++', 'Java', 'Perl', 'HTML, CSS', 'JavaScript']" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# สร้าง list ที่มีสมาชิกเป็นสตริง\n", "languages = [\"Python\", \"C\", \"C++\", \"Java\", \"Perl\", \"HTML, CSS\", \"JavaScript\"]\n", "languages" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[True, False]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# สร้าง list ที่มีสมาชิกเป็น boolean\n", "boolList = [True, False]\n", "boolList" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เราสามารถสร้าง List ที่ไม่มีสมาชิก (Empty list) ก็ได้ ดังนี้ " ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# List ที่ไม่มีสมาชิก (Empty list)\n", "empty = []\n", "empty" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Empty list มักถูกใช้เพื่อบันทึกผลระหว่างการรันโปรแกรม รายละเอียดและตัวอย่างการใช้งานจะอธิบายภายหลัง (ในหัวข้อเมธอด `append()`)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "สมาชิกของลิสต์ไม่จำเป็นต้องเป็นชนิดเดียวกัน" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['Katy Perry', 10.1, 2017]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mixedList = [\"Katy Perry\", 10.1, 2017]\n", "mixedList" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เราสามารถใช้เลขดัชนีปรกติ (Index) และ เลขดัชนีเชิงลบ (Negative index) เข้าถึงสมาชิกใน List ได้ เช่นเดียวกับ สตริงและ Tuple" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](images/ListsNeg.png)\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "We get the same element using negative and positive indexing:\n", " Postive: Katy Perry \n", " Negative: Katy Perry\n", "We get the same element using negative and positive indexing:\n", " Postive: 10.1 \n", " Negative: 10.1\n", "We get the same element using negative and positive indexing:\n", " Postive: 2017 \n", " Negative: 2017\n" ] } ], "source": [ "# ใช้คำสั่ง print แสดงสมาชิกแต่ละตัวที่อยู่ในลิสต์โดยใช้เลขดัชนี้ปรกติ(Index) และ เลขดัชนีเชิงลบ (Negative index)\n", "# ซึ่งจะได้ผลลัพธ์เหมือนกัน\n", "\n", "print('We get the same element using negative and positive indexing:\\n Postive: ',mixedList[0],\n", "'\\n Negative:' , mixedList[-3] )\n", "print('We get the same element using negative and positive indexing:\\n Postive: ',mixedList[1],\n", "'\\n Negative:' , mixedList[-2] )\n", "print('We get the same element using negative and positive indexing:\\n Postive: ',mixedList[2],\n", "'\\n Negative:' , mixedList[-1] )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "List ต่างกับ Tuple ตรงที่ เป็นข้อมูลที่เปลี่ยนแปลงได้ (Mutable) นั่นคือองค์ประกอบรายการที่จัดทำดัชนีสามารถแก้ไขได้โดยใช้โอเปอร์เรเตอร์ `=`" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['Maroon 5', 10.1, 2017]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mixedList[0] = \"Maroon 5\"\n", "mixedList" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "สมาชิกของ list นอกจากจะเป้นข้อมูลชนิดสตริง (str), จำนวนจริง (float) และจำนวนเต็ม (int) แล้ว ยังสามารถเป็นออบเจกต์ใดๆ ก็ได้ ไม่ว่าจะเป็น Tuple, List, Nested Tuple, Nested List หรือแม้แต่ข้อมูลที่มีโครงสร้างอื่นๆ ก็ได้" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['Katy Perry', 10.1, 2017, [1, 2], ('B', 2), [[1, 2]], ('B', 2)]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ตัวอย่าง list ที่มีสมาชิกเป็น str, float, int, list, tuple, nested list, nested tuple\n", "\n", "nestedList = [\"Katy Perry\", 10.1, 2017, [1, 2], (\"B\", 2), [[1, 2]], ((\"B\", 2))]\n", "nestedList" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "หลักการของเลขดัชนี (Index) ใช้กับ Nested Tuple/List ได้ (เหมือนกับ tuples)\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[1, 2]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nestedList[3]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nestedList[3][1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## ตัวดำเนินการของ Lists" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "### การเชื่อมด้วยโอเปอร์เรเตอร์ `+` (Concatenation operator)\n", "\n", "$\\qquad$ เราสามารถใช้โอเปอร์เรเตอร์ `+` เชื่อม lists เข้าด้วยกัน (ผลลัพธ์เป็น list)\n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 2, 3, 4, 3, 4, 5]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list1 = [1, 2, 3] \n", "list2 = [2, 3, 4]\n", "list3 = [3, 4, 5]\n", "list1 + list2 + list3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### การทำซ้ำด้วยโอเปอร์เรเตอร์ `*` (Repetition operator)\n", "\n", "$\\qquad$ เช่นเดียวกับสตริง ตัวดำเนินการ `*` จะทำการเชื่อมลิสต์ตัวเดิมซ้ำตามจำนวนเลขที่ระบุ (ผลลัพธ์เป็น list)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 1, 2, 3]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list1*2" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 1, 2, 3, 'cheese']" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list1*2+['cheese']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### โอเปอร์เรเตอร์เปรียบเทียบ (Comparison Operator)\n", "\n", "$\\qquad$ เราสามารถใช้ตัวดำเนินการเปรียบเทียบของไพธอน เช่น `<, >, ==, !=` เปรียบเทียบระหว่าง list กับ list \n", "อีกตัวได้\n", "\n", "โดย List ที่เปรียบเทียบกันต้องมีสมาชิกที่เป็นประเภทข้อมูลที่สามารถเปรียบเทียบกันได้ มิฉะนั้น จะเกิดข้อผิดพลาด เช่น [100] > ['A'] จะเกิดข้อผิดพลาด (เนื่องจาก int' กับ 'str' เป็นคนละประภทกัน เปรียบเทียบกันไม่ได้)\n", "\n", "\n", "ผลลัพธ์ของการเปรียบเทียบจะเป็น `True` หรือ `False` โดยจะเปรียบเทียบสมาชิกตัวแรกของแต่ละลิสต์ก่อน ถ้ามีค่าเท่ากัน จะเปรียบเทียบสมาชิกตัวถัดไปเรื่อยๆ ไป (เทียบเป็นคู่ๆ) จนกว่าจะเจอสมาชิกที่มีค่าไม่เท่ากัน หลังจากนั้นจะเปรียบเทียบสมาชิกที่มีค่าไม่เท่ากันว่า มากกว่า หรือน้อยกว่า หรือ.... ตามโอเปอร์เรเตอร์นั้นๆ " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ตัวอย่างต่อไปนี้ เป็นการเปรียบเทีบสิลต์ [3.1415, 1.6180, 2.7182 , 0] (เป็นค่าของ Pi, The Golden Ratio, Euler's Constant และ zero ตามลำดับ) ว่ามีค่ามากว่าลิสต์ [6.626068E-34, 6.0221515E23, 1.380650E23] (เป็นค่า Planck's Constant ($m^2kg/s$), Avogadro's Constant และ Boltzmann's Constant ($joule/K$)]) หรือไม่" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# [Pi, The Golden Ratio, Euler's Constant, zero] > [Planck's Constant (m^2 kg/s), Avogadro's Constant, Boltzmann's Constant (joule/K)]\n", "[3.1415, 1.6180, 2.7182 , 0] > [6.626068E-34, 6.0221515E23, 1.380650E23]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ผลลัพธ์เป็น `True` เนื่องจาก\n", "จะเปรียบเทียบสมาชิกตัวแรกของทั้งสองลิสต์ก่อน ซึ่ง 3.1415 ไม่เท่ากับ 6.626068E-34 จึงประมวลผลตามโอเปอร์เรเตอร์ ว่า 3.1415 > 6.626068E-34 หรือไม่ ทำให้ผลลัพธ์เป็น `True`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### การตัด (Slicing)\n", "\n", "$\\qquad$ เราสามารถตัด (Slicing) List ได้เช่นเดียวกับการตัด tuples โดยผลของการตัดยังคงเป็นลิสต์" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['Katy Perry', 10.1, 2017, 'KP', 1]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ตัวอย่าง List\n", "\n", "mixedList = [\"Katy Perry\", 10.1,2017,\"KP\",1]\n", "mixedList" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](images/ListsSlice.png)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ถ้าเราต้องการสมาชิกแค่สองตัวสุดท้าย เราจะใช้คำสั่งต่อไปนี้" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['KP', 1]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mixedList[3:5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "หรือจะเขียนโดยใช้ฟังก์ชัน `len()` แทน แบบนี้ก็ได้เช่นกัน" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['KP', 1]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mixedList[len(mixedList)-2:len(mixedList)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(ch3-2-membershipOperators)=\n", "### ตัวดำเนินการตรวจสอบสมาชิก (Membership operators)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\qquad$ ตัวดำเนินการที่ใช้ตรวจสอบว่า ค่าที่เราต้องการตรวจสอบมีอยู่ในออบเจ็กต์ปลายทางหรือไม่ มีอยู่ 2 ตัว คือ\n", "\n", "\n", " * **ตัวดำเนินการ `in`** ใช้ตรวจสอบว่า ค่าที่เราต้องการมีอยู่ในออบเจ็กต์ปลายทางใช่หรือไม่ โดยจะคืนค่าเป็น True ในกรณีที่มีค่าที่ระบุอยู่ในออบเจ็กต์ปลายทาง เช่น\n", "\n", "```python\n", "my_favorite_lang = [\"Python\", \"Swift\"]\n", "\n", "print(\"Python\" in my_favorite_lang) # คืนค่าเป็น True เพราะมี \"Python\" อยู่ในตัวแปร my_favorite_lang\n", "```\n", "\n", " * **ตัวดำเนินการ `not in`** ใช้ตรวจสอบว่า ไม่มีค่าที่เราระบุอยู่ในออบเจ็กต์ปลายทางใช่หรือไม่ โดยจะคืนค่าเป็น True ในกรณีที่ไม่มีค่าที่ระบุอยู่ในออบเจ็กต์ปลายทาง เช่น\n", "\n", "```python\n", "my_favorite_lang = [\"Python\", \"Swift\"]\n", "\n", "print(\"Kotlin\" not in my_favorite_lang) # คืนค่าเป็น True เพราะไม่มี \"Kotlin\" อยู่ในตัวแปร my_favorite_lang\n", "```" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# สร้างตัวแปรลิสต์\n", "pop_list = ['pop','pop rock','dance pop','electropop','teen pop']\n", "# ตรวจสอบว่าค่าที่ระบุเป็นสมาชิกของลิสต์ pop_list หรือไม่\n", "\"pop\" in pop_list" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ตรวจสอบว่าค่าที่ระบุเป็นสมาชิกของสตริงหรือไม่\n", "'!' in 'Hello python'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [Exercise] ลิสต์ - การสร้าง เลขดัชนีและตัวดำเนินการ" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`1` จงสร้างตัวแปรลิสต์ `frontend` ที่ประกอบด้วยสมาชิก \"HyperText Markup Language (HTML)\", \"Cascading Style Sheets (CSS)\", \"JavaScript\", \"React\", \"Angular\", \"React\" แล้วแสดงผลลัพธ์ออกหน้าจอ" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "tags": [] }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['HyperText Markup Language (HTML)', 'Cascading Style Sheets (CSS)', 'JavaScript', 'React', 'Angular', 'React']\n" ] } ], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n", "\n", "frontend = [\"HyperText Markup Language (HTML)\", \"Cascading Style Sheets (CSS)\", \"JavaScript\", \"React\", \"Angular\", \"React\"]\n", "print(frontend)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`2` จากข้อก่อนหน้า จงเขียนโค้ดเพิ่มภาษา \"Swift\" เข้าไปในลิสต์ `frontend` โดยใช้โอเปอร์เรเตอร์ `+` แล้วแสดงผลลัพธ์ออกหน้าจอ\n" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "tags": [] }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['HyperText Markup Language (HTML)', 'Cascading Style Sheets (CSS)', 'JavaScript', 'React', 'Angular', 'React', 'Swift']\n" ] } ], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n", "frontend = frontend + [\"Swift\"]\n", "# frontend.append(\"Swift\") # หรือจะใช้เมธอด append() ก็ได้ (รายละเอียดอยู่ในหัวข้อถัดไป)\n", "print(frontend)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`3` จากข้อก่อนหน้า จงเขียนโค้ดแสดงขนาดข้อมูลที่เก็บอยู่ในในลิสต์ `frontend` ออกหน้าจอ\n" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "tags": [] }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n" ] } ], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n", "print(len(frontend))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`4` จากข้อก่อนหน้า จงสร้างตัวแปร `secondElement` ที่เก็บค่าของสมาชิกตัวที่ 2 ของลิสต์ `frontend` แล้วแสดงผลออกหน้าจอ\n" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "tags": [] }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Cascading Style Sheets (CSS)\n" ] } ], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n", "secondElement = frontend[1]\n", "print(secondElement)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`5` จากข้อก่อนหน้า จงเขียนโค้ดแสดงเฉพาะตัวย่อ `CSS` ที่อยู่ในสมาชิกตัวที่ 2 ของตัวแปรลิสต์ `frontend` ออกหน้าจอ\n" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "tags": [] }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CSS\n" ] } ], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n", "print(frontend[1][24:27])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`6` จากข้อก่อนหน้า จงเขียนโค้ดตรวจสอบว่า 'Cascading Style Sheets (CSS)' และ 'CSS' เป็นสมาชิกของลิสต์ `frontend` หรือไม่\n" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "tags": [] }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Cascading Style Sheets (CSS) in frontend? True\n", "CSS in frontend? False\n" ] } ], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n", "print(\"Cascading Style Sheets (CSS) in frontend?\", 'Cascading Style Sheets (CSS)' in frontend)\n", "print(\"CSS in frontend?\",'CSS' in frontend)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ฟังก์ชันและเมธอดของ Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\qquad$ List มีฟังก์ชันและเมธอดมาตรฐาน (built-in functions/methods) หลากหลาย \n", "\n", "ฟังก์ที่ใช้ดำเนินการกับลิสต์เป็นฟังก์ชั่นที่อยู่ในไลบรารีมาตรฐานของ Python ช่วยทำให้เขียนโปรแกรมทำได้ง่ายขึ้น เร็วขึ้นและมีประสิทธิภาพมากขึ้น ฟังก์ชันของลิสต์ที่สำคัญๆ มีดังต่อไปนี้" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**การหาจำนวนสมาชิกด้วยฟังก์ชัน `len()`**\n", "\n", "$\\qquad$ `len()` เป็นฟังก์ชันที่ใช้หาจำนวนหรือขนาดของออบเจ็กต์ใดๆ ถ้าใช้กับลิสต์จะส่งกลับเป็นจำนวนสมาชิกที่อยู่ในสลิสต์ (ถ้าใช้กับ tuple และสตริง จะส่งค่ากลับเป็นจำนวนสมาชิกที่อยู่ในทูเพิล และจำนวนตัวอักขระในสตริง ตามลำดับ)\n", "\n", "***Syntax:*** \n", "\n", "```\n", "len(list)\n", "```\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ลิสต์ `numbers` มีขนาดหรือมีจำนวนสมาชิกอยู่ทั้งหมด 11 สมาชิก" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numbers = [0, 10, 20, 30, 40, 50, 60, 70, 80,90, 100]\n", "\n", "len(numbers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**การหาผลรวมด้วยฟังก์ชัน `sum()`**\n", "\n", "\n", "$\\qquad$ ฟังก์ชัน `sum()` ใช้หาผลรวมของสมาชิกที่อยู่ในลิสต์ที่กำหนด ใช้ได้เฉพาะกับลิสต์มีค่าเป็นตัวเลขเท่านั้น\n", "\n", "\n", "***Syntax:*** \n", "\n", "```\n", "sum(numeric_list))\n", "```\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ผลรวมของสมาชิกที่อยู่ในลิสต์ `numbers` คือ 550" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "550\n" ] } ], "source": [ "print(sum(numbers)) # 0 + 10 + 20 + 30 + 40 + 50 + 60 + 70 + 80 + 90 + 100 ผลรวมเท่ากับ 550" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**การหาค่ามากสุดและน้อยสุดด้วยฟังก์ชัน `max(), min()`**\n", "\n", "$\\qquad$ ฟังก์ชัน `max()` และ `min()` จะส่งค่ากลับเป็นสมาชิกที่อยู่ในลิสต์ที่มีค่ามากที่สุด และน้อยที่สุดที่ ตามลำดับ โดยสมาชิกที่อยู่ในลิสต์ต้องเป็นข้อมูลที่สามารถเปรียบเทียบกันได้ เช่น มีสมาชิกเป็นตัวเลข max([5,10.9,11,0]): 11 หรือมีสมาชิกเป็นสตริง min(['apple', 'mango', 'banana']): 'apple' แต่ถ้าสมาชิกเป็นตัวเลขปนกับสตริง [5,'apple',11,'mango'] จะเปรียบเทียบกันไม่ได้ (เกิด Error)\n", "\n", "**Syntax:***\n", "\n", "```\n", "max(list)\n", "```\n", "\n", "```\n", "min(list)\n", "``` \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ลิสต์ `numbers` ข้างต้น มีสมาชิกที่มีค่ามากที่สุดและน้อยที่สุดคือ 100 และ 0 ตามลำดับ" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "max(numbers) = 100\n", "min(numbers) = 0\n" ] } ], "source": [ "print(\"max(numbers) =\", max(numbers))\n", "print(\"min(numbers) =\", min(numbers))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "คำสั่งข้างต้นเป็นการเขียนโดยการส่ง `max(numbers)` และ `min(numbers)` เป็นอาร์กิวเมนต์ให้กับฟังก์ชัน `print()` แต่เราสามาระเขียนเป็น f-string ในรูปแบบ `f'{expr=}'` แบบนี้ก็ได้ (รายละเอียดของ f-string อยู่ในหัวข้อ [การใช้ฟังก์ชัน print( ) แสดงผลของตัวแปร (นิพจน์)](ch1_printFunction))" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "max(numbers) = 100\n", "min(numbers) = 0\n" ] } ], "source": [ "print(f\"{max(numbers) = }\")\n", "print(f\"{min(numbers) = }\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**การแปลงข้อมูลด้วยฟังก์ชัน `list()`**\n", "\n", "$\\qquad$ ฟังก์ชัน `list()` เป็นฟังก์ชันที่ใช้แปลงข้อมูลประเภทลำดับ (sequence หรือ iterable) เช่น สตริง (str) ทูเพิล (tuple) ดิกชันนารี (dict) ให้เป็นข้อมูลประเภท List \n", "\n", "**Syntax:**\n", "\n", "```\n", "list(iterable)\n", "```\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Iterable[^iterable] คือ ออบเจกต์ใดๆ ที่สามารถทำการวนซ้ำกับมันได้ (ออบเจกต์ที่ไล่วนลูปซ้ำๆ ได้) ยกตัวอย่างเช่น Lists, Tuples หรือแม้แต่ Strings ก็ถือเป็น Iterable เช่นกัน\n", "\n", "[^iterable]: *Iterate (iter-able)* adjective สามารถทำซ้ำได้ noun: (programming) ออบเจ็กต์ที่สามารถทำการวนซ้ำได้\n" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 'b', 'c', 'd', 'e', 'f', 'g']\n", "[5, 12, 13]\n", "['a', 'b', 'c']\n", "list(str1) = ['a', 'b', 'c', 'd', 'e', 'f', 'g']\n" ] } ], "source": [ "str1= \"abcdefg\" # ตัวแปรชนิด str\n", "tuple1=(5, 12, 13) # ตัวแปรชนิด tuple\n", "dict1={ \"a\":1, \"b\":2, \"c\":3} # ตัวแปรชนิด dict\n", "\n", "print(list(str1)) # แปลงข้อมูล str ให้เป็น list\n", "print(list(tuple1)) # แปลงข้อมูล tuple ให้เป็น list\n", "print(list(dict1)) # แปลงข้อมูล dict ให้เป็น list\n", "print(f\"{list(str1) = }\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{important} \n", "**ฟังก์ชัน list()** ใช้แปลงข้อมูลจากข้อมูลที่เปลี่ยนแปลงไม่ได้ (Immutable datatypes) ให้เป็นข้อมูลที่เปลี่ยนแปลงได้ (Mutable datatypes) ได้\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**การค้นหาสมาชิกด้วยเมธอด Index( )**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\qquad$ เมธอด `index( )` ใช้ค้นหาสมาชิกที่อยู่ในลิสต์ โดยส่งกลับเป็นเลขดัชนีของสมาชิกในลิสต์ (ในกรณีที่มีค่าของสมาชิกมีซ้ำกัน จะส่งคืนเฉพาะตำแหน่งแรกที่เจอ และในกรณีที่ไม่มีอยู่ในลิสต์ จะเกิด Error)\n", "\n", "\n", "**Syntax:**\n", "\n", "```\n", "list.index(element)\n", "```\n", "\n", "โดยคืนค่ากลับเป็นเลขดัชนีของสมาชิก (element) ที่ต้องการค้นหา " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "'KP' อยู่ในลิสต์ `mixedList` และอยู่ในตำแหน่งที่ 4 (เลขดัชนีเป็น 3)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mixedList = [\"Katy Perry\", 10.1,2017,\"KP\",1]\n", "mixedList.index('KP')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เนื่องจาก List จัดเก็บข้อมูลเป็นลำดับ\n", "เราสามารถจัดเรียงลำดับ List ย้อนกลับ (จากสมาชิกตัวท้ายสุดย้อนกลับมาตัวแรก) ได้โดยใช้วิธีเดียวกับสตริงและ Tuple" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "[1, 'KP', 2017, 10.1, 'Katy Perry']" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Syntax เรียงลำดับ List ย้อนกลับ ซึ่งเป็นวิธีเดียวกับสตริงและ Tuple\n", "mixedList[::-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "'KP' ยังคงอยู่ในลิสต์ `mixedList[::-1]` แต่อยู่ในตำแหน่งที่ 2 (เลขดัชนีคือ 1)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mixedList[::-1].index('KP')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**การเพิ่มสมาชิก (หลายตัว) ด้วยเมธอด extend( )**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\qquad$ เราสามารถใช้เมธอด `extend( )` **เพิ่มสมาชิกหลายตัวต่อท้าย** List ที่ต้องการได้\n", "\n", "\n", "**Syntax:**\n", "\n", "```\n", "list.extend(iterable)\n", "```\n", "\n", "โดยเพิ่ม iterable (list, tuple, str ฯลฯ) ต่อท้ายลิสต์ (เป็นการเพิ่มสมาชิกใหม่**หลายตัว**ต่อท้าย)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['Katy Perry', 10.2, 'pop', 10]" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ใช้เมธอด extend() เพิ่มสมาชิกหลายตัวให้กับลิสต์\n", "L = [ \"Katy Perry\", 10.2]\n", "L.extend(['pop', 10])\n", "L" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`extend()` จะเป็นการเพิ่มสมาชิกใหม่สองตัว (ของลิสต์ `['pop', 10]`) เข้าไปใน List `L`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**การเพิ่มสมาชิก (สมาชิกเดียว) ด้วยเมธอด append()**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\qquad$ อีกเมธอดที่คล้ายกันคือ `append()` ถ้าเราใช้ `append()`[^append] แทน `extend()`[^extend] จะเป็นการ**เพิ่มสมาชิกแค่สมาชิกเดียว (Nested List)**\n", "\n", "**Syntax:**\n", "\n", "```\n", "list.append(item)\n", "```\n", "โดยเพิ่ม item (ตัวเลข สตริง ลิสต์ ทูเพิล ฯลฯ) ต่อท้ายลิสต์ (เป็นการเพิ่มสมาชิกใหม่**แค่สมาชิกเดียว**ต่อท้าย)\n", "\n", "[^append]: *append* verb: ผนวกเข้า,ห้อยท้าย\n", "[^extend]: *extend* verb: ขยายออก, ยืดออก" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['Katy Perry', 10.2, ['pop', 10]]" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ใช้ append เพิ่มสมาชิกให้กับลิสต์\n", "L = [ \"Katy Perry\", 10.2]\n", "L.append(['pop', 10])\n", "L" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`append()` จะเป็นการเพิ่มสมาชิกใหม่เพียงตัวเดียว (ลิสต์ `['pop', 10]`) เข้าไปใน List `L` (กลายเป็น Nested List)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ถ้าเราสังเกต ทุกครั้งที่มีการเรียกใช้เมธอด extend() หรือ append() ค่าที่เก็บไว้ใน List จะเปลี่ยน! " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**ความแตกต่างระหว่าง `extend()` และ `append()`**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "หากถ้าต้องการเพิ่มสมาชิกหลายตัวต่อท้าย List เช่น เพิ่มสมาชิกแต่ละตัวที่อยู่ในทูเพิล `b` (3,4) ต่อท้าย List `a` [1,2] ให้ใช้เมธอด `extend()`\n" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a.extend((3,4)): [1, 2, 3, 4]\n", "a.append((3,4)): [1, 2, (3, 4)]\n" ] } ], "source": [ "a = [1, 2]\n", "b = (3,4)\n", "a.extend(b) # a จะเปลี่ยนเป็น [1, 2, 3, 4]\n", "print('a.extend((3,4)): ', a)\n", "\n", "a = [1, 2]\n", "b = (3,4)\n", "a.append(b) # a จะเปลี่ยนเป็น [1, 2, (3, 4)]\n", "print('a.append((3,4)): ',a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "แต่ถ้าหากต้องการเพิ่มสมาชิกเพียงแค่สมาชิกเดียวต่อท้าย List เช่น เพิ่มสตริงค์ `'30-Nov-17'` เป็นสมากชิกใหม่ (เพื่มทั้งสตริงค์ให้เป็นสมาชิกเดียว) ต่อท้ายลิสต์ `L` ให้ใช้เมธอด `extend()`\n" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "L.extend('30-Nov-17'): ['Katy Perry', 10.2, '3', '0', '-', 'N', 'o', 'v', '-', '1', '7']\n", "L.append('30-Nov-17'): ['Katy Perry', 10.2, '30-Nov-17']\n" ] } ], "source": [ "L = [\"Katy Perry\", 10.2]\n", "date = '30-Nov-17'\n", "L.extend(date)\n", "print(\"L.extend('30-Nov-17'): \", L)\n", "\n", "L = [\"Katy Perry\", 10.2]\n", "date = '30-Nov-17'\n", "L.append(date)\n", "print(\"L.append('30-Nov-17'): \", L)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ผลลัพธ์ของโค้ด `L.extend('30-Nov-17')` ข้างต้นและผลลัพธ์ต่อไปนี้เป็นการเพิ่มทีละตัวอักษรของข้อความ '30-Nov-82' แทนที่จะเป็นแค่ข้อความเดียว เนื่องจากเป็น str ซึ่งเป็นข้อมูลชนิด Iterable" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['Katy Perry', 10.2, '3', '0', '-', 'N', 'o', 'v', '-', '1', '7']" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L = [\"Katy Perry\", 10.2]\n", "L += '30-Nov-17' # L += ['30-Nov-82'] # เพิ่มทีละตัวอักษร\n", "L" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***************************" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\qquad$ Tuple จะคล้ายกับ List แต่สิ่งที่แตกต่างกันคือ Tuple นั้นเป็นประเภทข้อมูลที่ไม่สามารถเปลี่ยนแปลงได้ (Immutable)[^Immutable] ในขณะที่ **List เป็นประเภทข้อมูลที่สามารถเปลี่ยนแปลงได้ (Mutable)[^Mutable]**\n", "\n", "เราสามารถเปลี่ยนค่าของสมาชิกใน List ได้ ดังตัวอย่างต่อไปนี้\n", "\n", "[^Mutable]: Mutable Objects เช่น Int, Float, String, Tuple, Frozen Set\n", "[^Immutable]: Immutable Objects เช่น List, Dictionary, Set\n" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Before change: ['dance', 10, 1.2]\n", "After change: ['pop rock', 10, 1.2]\n" ] } ], "source": [ "# เปลี่ยนค่าของสมาชิกโดยใช้เลขดัชนี\n", "\n", "A = [\"dance\", 10, 1.2]\n", "print('Before change:', A)\n", "A[0] = 'pop rock'\n", "print('After change:', A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "จะเปลี่ยนค่าของสมาชิกทีละหลายๆ ค่าพร้อมกันก็ได้ ดังตัวอย่างต่อไปนี้ (เปลี่ยน 2 ตัวแรก)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Before change: ['dance', 10, 1.2]\n", "After change: ['pop rock', 9, 1.2]\n" ] } ], "source": [ "# การแก้ไขค่าของสมาชิกหลายๆ ตัวพร้อมกัน\n", "A = [\"dance\", 10, 1.2]\n", "print('Before change:', A)\n", "A[0:2] = 'pop rock',9\n", "print('After change:', A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**การเปลี่ยนสตริงให้เป็น list**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\qquad$ เราสามารถแปลงข้อความ(สตริง)ให้เป็น List ได้โดยใช้เมธอดสตริง `split( )` \n", "\n", "เมธอด `split( )` จะตัดสตริงยาวๆ ออกเป็นคำๆ (Words) ถ้าไม่กำหนดตัวอักษรในการตัด แต่ละคำถูกจะถูกตัดด้วยอักขระ *space (เว้นวรรค)* โดยปริยาย และคำแต่ละคำที่ถูกตัดจะถูกเก็บเป็นสมาชิกของ **List**" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['pop', 'rock']" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# การตัดสตริงค์ด้วยเมธอด split( ) มีค่าเริ่มต้นในการตัด คือ space\n", "\n", "'pop rock'.split()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เราสามารถกำหนดอักขระที่เป็น **ตัวคั่น (Delimiter)** เองก็ได้ โดยการส่งอาร์กิวเมนต์ระบุตัวคั่นให้กับฟังก์ชัน `split( )`\n", "\n", "\n", "ตัวอย่างต่อไปนี้ เป็นการกำหนดตัวคั่นเป็นเครื่องหมายจุลภาค **,** (comma) " ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['A', 'B', 'C', 'D']" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# การตัดสตริงค์ด้วยเครื่องหมายจุลภาค\n", "\n", "'A,B,C,D'.split(',')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**การลบข้อมูลด้วยคีย์เวิร์ด del**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\qquad$ `del` [^del] เป็นคีย์เวิร์ดในภาษาไพธอน ย่อมาจาก **del**ete เราใช้คีย์เวิร์ด `del` ลบออบเจ็กต์ ดังนั้นสามารถใช้เพื่อลบตัวแปรที่เป็น int float str tuple list ฯลฯ ได้ \n", "\n", "เช่น ตัวแปร `A` ข้างต้นเป็นลิสต์ และมีค่าเป็น" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['pop rock', 9, 1.2]\n" ] } ], "source": [ "print(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "แต่ถ้าเรารันคำสั่ง `del A` แล้วรันคำสั่งเดิมซ้ำ จะเกิดข้อผิดพลาด (NameError) เนื่องจากตัวแปร `A` ถูกลบทิ้งไปแล้ว" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "tags": [] }, "outputs": [ { "ename": "NameError", "evalue": "name 'A' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn [52], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m A\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mA\u001b[49m)\n", "\u001b[0;31mNameError\u001b[0m: name 'A' is not defined" ] } ], "source": [ "del A\n", "print(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[^del]: โดยทั่วไป คนส่วนใหญ่เข้าใจว่า คีย์เวิร์ด del ใช้ในการลบออบเจ็กต์ออกจากหน่วยความจำ แต่ในความเป็นจริง `del` ไม่ได้ลบออบเจ็กต์ และเป็นการลบชื่อตัวแปร ออกจากเนมสเปซ (Namespaces)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "นอกจากนี้ เรายังสามารถใช้คีย์เวิร์ด `del` ลบเฉพาะบางสมาชิกที่เข้าถึงได้ด้วย index เช่น สมาชิกของ List ได้อีกด้วย (แต่ไม่สามารถลบตัวอักษรของ string หรือสมาชิกของ tuple ได้ เนื่องจากทั้งคู่เป็นออบเจ็กต์ชนิด immutables (แต่ยังคง สามารถลบทั้งออบเจ็กต์ทิ้งได้)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# การลบสมาชิกโดยใช้เลขดัชนี\n", "A = [\"dance\", 10, 1.2]\n", "print('Before change:', A)\n", "del(A[0])\n", "print('After change:', A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "และยังสามารถลบสมาชิกได้หลายตัวโดยใช้การตัด (Slicing)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# การลบสมาชิกหลาบๆ ตัวโดยใช้การตัด (Slicing)\n", "A = [\"dance\", 10, 1.2]\n", "print('Before change:', A)\n", "del(A[0:2])\n", "print('After change:', A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## การก๊อปปี้ (Copy) / การโคลนนิ่ง (Cloning) Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\qquad$ กรณีที่เรากำหนดตัวแปร `B` ให้เท่ากับตัวแปรลิสต์ `A` (โดยใช้โอเปอเรเตอ์ `=`; `B = A`) ตัวแปรลิสต์ทั้งสอง `A` และ `B` จะอ้างอิง (Reference) ข้อมูล List ตัวเดียวกันในหน่วยความจำ\n" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A: ['pop rock', 10, 1.2]\n", "B: ['pop rock', 10, 1.2]\n" ] } ], "source": [ "# การก๊อปปี้ (การก๊อปปี้โดยการอ้างอิง) ลิสต์ A\n", "A = [\"pop rock\", 10, 1.2]\n", "B = A\n", "print('A:', A)\n", "print('B:', B)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](images/ListsRef.png)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เนื่องจากตัวแปรลิสต์ทั้งสองอ้างอิง List ตัวเดียวกัน (ชี้ไปยัง List ตัวเดียวกัน) ดังนั้น ถ้าตัวแปรตัวใดตัวหนึ่งมีการเปลี่ยนค่า ตัวแปรตัวที่เหลือค่าก็จะเปลี่ยนไปด้วย\n" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "B[0]: pop rock\n", "B[0]: banana\n" ] } ], "source": [ "# ตรวจสอบการก๊อปปี้โดยการอ้างอิง ข้อมูลของตัวแปรไม่เป็นอิสระต่อกัน\n", "\n", "print('B[0]:', B[0])\n", "A[0] = \"banana\"\n", "print('B[0]:', B[0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ดังแสดงในรูปต่อไปนี้" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](images/ListsRef2.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ฉะนั้น การก๊อปปี้ตัวแปรลิสต์ด้วยโอเปอร์เรเตอร์ `=` จึงไม่ได้เป็นการก๊อปปี้ด้วยค่าของข้อมูล (Copy by value) แต่เป็นการก๊อปปี้โดยการอ้างอิง (Copy by reference) กล่าวคือ เป็นเพียงการกำหนดตำแหน่งในหน่วยความจำให้กับตัวแปร เท่านั้น\n", "\n", "\n", "อย่างไรก็ตาม เราสามารถก๊อปปี้ค่าของข้อมูลหรือที่เรียกว่า การโคลนนิ่ง (Cloning) ลิสต์ได้หลากหลายวิธี แต่วิธีที่ง่ายที่สุดคือการใช้การตัด (slicing) ลิสต์ โดยมี syntax ดังนี้\n", "\n", "\n", "**Syntax:**\n", "\n", "```\n", "new_list = old_list[:]\n", "```\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['pop rock', 10, 1.2]" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# การโคลนนิ่ง (Cloning) ลิสต์ A โดยการตัด (Slicing)\n", "A = [\"pop rock\", 10, 1.2]\n", "B = A[:]\n", "B" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ตัวแปร **B** จะอ้างอิงกับ **List ใหม่**ที่โคลนค่าของสมาชิกทุกตัวที่อยู่ใน List ต้นฉบับ" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](images/ListsVal.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ในกรณีนี้ตัวแปลทั้งสองจะเป็นอิสระต่อกัน ลองเปลี่ยนลิสต์ `A` ลิสต์ `B` จะไม่เปลี่ยนตาม" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A=['pop rock', 10, 1.2]\n", "B=['pop rock', 10, 1.2]\n", "After running this code: A[0] = \"banana\"\n", "A=['banana', 10, 1.2]\n", "B=['pop rock', 10, 1.2]\n" ] } ], "source": [ "print(f'{A=}')\n", "print(f'{B=}')\n", "A[0] = \"banana\"\n", "print('After running this code: A[0] = \"banana\"')\n", "print(f'{A=}')\n", "print(f'{B=}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note} \n", "การโคลน (Cloning) เป็นการสร้าง**ออบเจ็กต์ตัวใหม่**ขึ้นมา ดังนั้น List `A` กับ List `B` เป็นอิสระต่อกัน ถือเป็นออบเจ็กต์คนละตัวกัน\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [Exercise] ฟังก์ชันและเมธอดของลิสต์" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`1.` การสร้างตัวแปรลิสต์และการเข้าถึงข้อมูลของลิสต์\n", "\n", "1.1) จงสร้างตัวแปร List `a_list` ทีมีสมาชิกต่อไปนี้ `1`, `'hello'`, `[1,2,3]` และ `True`\n" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "tags": [] }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]: #\"\n", "
Click here for the solution\n", "\n", "```{python}\n", "a_list = [1, 'hello', [1, 2, 3] , True]\n", "a_list\n", "\n", "```\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`1.2)` จงเขียนโค้ดเข้าถึงสมาชิกที่มีเลขดัชนีเป็น 1\n" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "tags": [] }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]:#\"\n", "
Click here for the solution\n", "\n", "```python\n", "a_list[1]\n", "\n", "```\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`1.3)` จงดึงสมาชิกที่มีเลขดัชนีเป็น 1, 2 และ 3 ออกจาก `a_list`\n" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "tags": [] }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]: #\"\n", "
Click here for the solution\n", "\n", "```python\n", "a_list[1:4]\n", "\n", "```\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`2.` จงเขียนโค้ดรวมสมาชิกที่อยู่ในลิสต์ `A = [1, 'a']` และสมาชิกที่อยู่ในลิสต์ `B = [2, 1, 'd']` เข้าด้วยกัน" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "tags": [] }, "outputs": [], "source": [ "# เขียนโค้ดต่อด้านล่าง แล้วกด Shift+Enter\n", "A = [1, 'a'] \n", "B = [2, 1, 'd']\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]: #\"\n", "
Click here for the solution\n", "\n", "```python\n", "A = [1, 'a'] \n", "B = [2, 1, 'd']\n", "A + B\n", "```\n", " \n", "หรือ\n", " \n", "```python\n", "A = [1, 'a'] \n", "B = [2, 1, 'd']\n", "A.extend(B)\n", "print(A)\n", "```\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`3.` กำหนดให้ \n", "`C = [5, 22/7, 3.14159, True]` และ `D = [True, 5, 22/7, 3.14159]`\n", "\n", "จงเขียนโค้ดตรวจสอบผลลัพธ์ของ C กับ D ว่าเท่ากันหรือไม่" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "tags": [] }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]: #\"\n", "
Click here for the solution\n", "\n", "```python\n", "C = [5, 22/7, 3.14159, True]\n", "D = [True, 5, 22/7, 3.14159]\n", "C == D\n", "```\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`4.` จากข้อก่อนหน้า จงเขียนโค้ดตรวจสอบผลลัพธ์ของ C **หลังเรียงลำดับจากน้อยไปมาก** กับ D **หลังเรียงลำดับจากน้อยไปมาก** ว่าเท่ากันหรือไม่" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "tags": [] }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]: #\"\n", "
Click here for the solution\n", "\n", "```python\n", "C.sort()\n", "D.sort()\n", "C == D\n", "```\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`5.` จากข้อก่อนหน้า จงเขียนโค้ดตรวจสอบผลลัพธ์ว่า '22/7' เป็นสมาชิกของ C หรือไม่" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "tags": [] }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]: #\"\n", "\n", "
Click here for the solution\n", "\n", "```python\n", "print('22/7' in C)\n", "```\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": { "jp-MarkdownHeadingCollapsed": true, "tags": [] }, "source": [ "`6.` กำหนดให้ตัวแปรสตริงค์ `quote` เก็บข้อความต่อไปนี้ \n", "\n", "```\n", "quote = 'Where There is a Will There is a Way'\n", "```\n", "\n", "จงตัดข้อความข้างต้นให้เป็นคำๆ เป็นลิสต์ " ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "tags": [] }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]: #\"\n", "\n", "
Click here for the solution\n", "\n", "```python\n", "newton_quote = 'Where There is a Will There is a Way'\n", "newton_quote.replace(',', '').replace(';', '').split()\n", "\n", "```\n", "\n", "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Author\n", "\n", "S.C.\n", "\n", "### Change Log\n", "\n", " \n", "| Date | Version | Change Description |\n", "|---|---|---|\n", "| 08-08-2021 | 0.1 | First edition |\n", "| 05-05-2022 | 0.2 | Fix the typo, minor change |\n", "| | | |\n", "| | | |\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel(3.8.13))", "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.13" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": { "height": "calc(100% - 180px)", "left": "10px", "top": "150px", "width": "193.913px" }, "toc_section_display": true, "toc_window_display": false }, "vscode": { "interpreter": { "hash": "e921dbf9f2d20a8528fa054ff97aae3eb23f1abc9a6f29b7bcee4307a5b88bad" } } }, "nbformat": 4, "nbformat_minor": 4 }