{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "![](images/python_with_Birds.gif)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "(ch5)=\n", "# Dictionaries (พจนานุกรม)\n", "\n", "ในบทนี้ คุณจะได้เรียนรู้เกี่ยวกับโครงสร้างข้อมูล Dictionary ในภาษา Python เราจะแนะนำให้คุณรู้จักกับ Dictionary การประกาศและการใช้งาน นอกจากนี้ เรายังจะแนะนำการใช้งานเมธอดและ built-in functions ของ Dictionary และตัวอย่างการใช้งานกับการเขียนโปรแกรมในรูปแบบต่างๆ ในภาษา Python\n", "\n", "\n", "\n", "Ref: \n", "* https://docs.python.org/3/tutorial/datastructures.html#dictionaries\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionaries คือ?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\qquad$ **Dictionary** เป็นข้อมูลประเภท Collections (หรือ Container) เช่นเดียวกับ List แต่จัดเก็บชุดข้อมูลแบบไม่เรียงลำดับ (non-sequential collections) โดยข้อมูลที่จัดเก็บอยู่ในรูปแบบของ Key-Value เป็นคู่ๆ (เทียบได้กับ HashMap ในภาษา Java หรือ โครงสร้างข้อมูลยอดฮิตแบบ JSON)\n", "\n", "* **Key**: (คีย์) ระบุชื่อของข้อมูลสำหรับอ้างอิง, \n", "* **Value**: (แวลยูว) เป็นค่าของข้อมูล (information) ที่ต้องการเก็บ \n", "\n", "มักจะใช้เก็บข้อมูลซึ่งมีขนาดใหญ่และจำเป็นต้องมีการค้นหาข้อมูล (ข้อมูลชนิดนี้จัดเก็บในรูปแบบของ Key-Value เป็นคู่ๆ เหมือนพจนานุกรมจริงๆ สอดคล้องกับชื่อ Dictionary !)\n", "โดย **Key** เป็นออบเจกต์/ข้อมูลที่แก้ไขไม่ได้ (Immutable type) เช่น สตริง (str) ตัวเลข(int, float) และทูเพิล (tuple) ดังนั้น คุณไม่สามารถใช้ออบเจกต์/ข้อมูลที่แก้ไขได้ (Mutable type) เช่น ลิสต์ (list) Dictionary (dict) เป็น Key ได้ (Dictionary เป็นชนิดข้อมูลที่แก้ไขได้) ในทางกลับกัน **Value** เป็นออบเจกต์/ข้อมูลชนิดใดก็ได้ จะแก้ไขได้หรือไม่ได้ก็ได้\n", "\n", "\n", "รูปด้านล่างเปรียบเทียบระหว่าง List กับ Dictionary\n", "\n", "List ใช้ Index หรือเลขดัชนีในการเข้าถึงข้อมูล แต่ Dictionary ใช้ Key ในการเข้าถึงข้อมูล\n", "**โดย Value เป็นค่าข้อมูลที่สอดคล้องกับ Key นั้นๆ**\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]: # (\n", "\n", ")\n", "\n", "![](images/DictsList.png)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ตัวอย่างของ Dictionary (`dict`):\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "{'key1': 1,\n", " 'key2': '2',\n", " 'key3': [3, 3, 3],\n", " 'key4': (4, 4, 4),\n", " 'key5': 5,\n", " 9.9: '๙',\n", " (0, 1): 6}" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# สร้างตัวแปร dictionary\n", "\n", "DicA = {\"key1\": 1, \"key2\": \"2\", \"key3\": [3, 3, 3], \"key4\": (4, 4, 4), ('key5'): 5, 9.9: '๙', (0, 1): 6}\n", "DicA" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(DicA)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(DicA)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ตัวแปร `DicA` ข้างต้นเป็นข้อมูลชนิด `dict` มีข้อมูล (สมาชิก) อยู่ในรูป key: value เป็นคู่ๆ ทั้งหมด 7 ข้อมูล โดย key 5 ตัวแรกเป็นชนิด str key ถัดมาเป็นตัวเลขชนิด float และ tuple ตามลำดับ" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "key ของ dictionary เป็นสตริง (str) ได้ เราสามารถเข้าถึงข้องมูลได้ด้วย key (เข้าถึงข้อมูลโดยใช้วงเล็บก้ามปู `[]` เช่นเดียวกับการเข้าถึงโดยใช้เลขดัชนี (index) ของ list)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# เข้าถึงข้อมูลด้วย key (สตริง)\n", "\n", "DicA[\"key1\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "key เป็นตัวเลข (int, float) ได้" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'๙'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# เข้าถึงข้อมูลด้วย key (ตัวเลข)\n", "\n", "DicA[9.9]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "key ยังสามารถเป็นออบเจ็กต์ที่ไม่เปลี่ยนรูป (immutable object) เช่น Tuple ก็ได้" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ข้าถึงข้อมูลด้วย key (Tuple)\n", "\n", "DicA[(0, 1)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## การสร้าง Dictionary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\qquad$ Dictionary ถูกสร้างขึ้นด้วยวงเล็บปีกกาสองอัน `{ }` (curly braces/brackets) โดยภายในวงเล็บประกอบไปด้วย key และ value เป็นคู่ๆ โดยคั่นแต่ละคู่ด้วยเครื่องหมายโคลอน (colon) ```:``` (ในรูปแบบ ```key: value```) และคั่นระหว่างสมาชิกด้วยเครื่องหมายคอมม่า (comma) `,`\n", "\n", "**Syntax** \n", "```\n", "{: , : , ...}\n", "```\n", "\n", "\n", " \n", "```(python)\n", "dic_var = {\n", " : ,\n", " : ,\n", " .\n", " .\n", " .\n", " : \n", "}\n", "```\n", "\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "{'Thriller': '1982',\n", " 'Back in Black': '1980',\n", " 'The Dark Side of the Moon': '1973',\n", " 'The Bodyguard': '1992',\n", " 'Bat Out of Hell': '1977',\n", " 'Their Greatest Hits (1971-1975)': '1976',\n", " 'Saturday Night Fever': '1977',\n", " 'Rumours': '1977'}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# สร้าง dictionary \n", "\n", "release_year_dict = {\"Thriller\": \"1982\", \"Back in Black\": \"1980\", \\\n", "\"The Dark Side of the Moon\": \"1973\", \"The Bodyguard\": \"1992\", \\\n", "\"Bat Out of Hell\": \"1977\", \"Their Greatest Hits (1971-1975)\": \"1976\", \\\n", "\"Saturday Night Fever\": \"1977\", \"Rumours\": \"1977\"}\n", "release_year_dict" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Thriller': '1982',\n", " 'Back in Black': '1980',\n", " 'The Dark Side of the Moon': '1973',\n", " 'The Bodyguard': '1992',\n", " 'Bat Out of Hell': '1977',\n", " 'Their Greatest Hits (1971-1975)': '1976',\n", " 'Saturday Night Fever': '1977',\n", " 'Rumours': '1977'}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# สร้าง dictionary\n", "\n", "release_year_dict = {\"Thriller\": \"1982\", \n", " \"Back in Black\": \"1980\",\n", "\"The Dark Side of the Moon\": \"1973\", \n", " \"The Bodyguard\": \"1992\",\n", "\"Bat Out of Hell\": \"1977\", \n", " \"Their Greatest Hits (1971-1975)\": \"1976\",\n", "\"Saturday Night Fever\": \"1977\", \n", " \"Rumours\": \"1977\"}\n", "release_year_dict" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{admonition} **Dictionary จัดเก็บข้อมูล (ออบเจกต์) เป็นชุดข้อมูล โดยไม่มีลำดับของข้อมูลที่เก็บ แต่จะเข้าถึง/แสดงข้อมูลแต่ละตัว (หรือ สมาชิก) ด้วย key และค่าที่สอดคล้องกัน (value)**\n", "\n", "* key แต่ละคีย์จะมี value ได้เพียงค่าเดียว (key ต้องไม่ซ้ำกัน) ในขณะที่\n", "* key มากกว่าหนึ่งคีย์สามารถมี value ค่าเดียวกันได้ (value ซ้ำกันได้)\n", "* key เป็นสตริง (str), ตัวเลข (int, float) หรือ Tuple (ข้อมูล immutable type) ได้เท่านั้น ในขณะที่ \n", "* value เป็นออบเจกต์ชนิดใดก็ได้ (ข้อมูล immutable, mutable และซ้ำกันก็ได้)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ข้อมูลในตัวแปร `release_year_dict` ข้างต้น สามารถสรุปเป็นตารางได้ดังนี้\n", "โดยคอลัมน์แรกเป็น `keys` และคอลัมน์ที่สองเป็น `values` ที่สอดคล้องกับ keys\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]: # (\n", "\n", ")\n", "\n", "![](images/DictsStructure.png)\n", "\n", "_คลิ๊กฟังเพลง_\n", "* [AC/DC - Back In Black(1980)](https://www.youtube.com/watch?v=pAgnJDJN4VA), \n", "* [Pink Floyd - The Dark Side of the Moon (1973)](https://www.youtube.com/watch?v=DLOth-BuCNY)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**นอกจากนี้ยังสามารถสร้างตัวแปร Dictionary โดยใช้ built-in ฟังก์ชัน `dict( )`**\n", "\n", "อาร์กิวเมนต์ที่ส่งไปให้ฟังก์ชั่น `dict([iterable, **kwarg] )` ต้องจัดเรียง key-value เป็นคู่ๆ เช่น **List ของ Tuple**\n", "\n", "```(python)\n", "dic_var = dict([\n", " (, ),\n", " (, ),\n", ".\n", ".\n", ".\n", " (, )\n", "])\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ลองสร้าง dictionary `release_year_dict` ข้างต้นใหม่อีกครั้งโดยใช้ built-in **ฟังก์ชัน `dict( )`**\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Thriller': '1982',\n", " 'Back in Black': '1980',\n", " 'The Dark Side of the Moon': '1973',\n", " 'The Bodyguard': '1992',\n", " 'Bat Out of Hell': '1977',\n", " 'Their Greatest Hits (1971-1975)': '1976',\n", " 'Saturday Night Fever': '1977',\n", " 'Rumours': '1977'}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n", "release_year_dict" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]: # \" \n", "
Click here for the solution\n", "\n", "```\n", "release_year_dict = dict([('Thriller', '1982'),\n", " ('Back in Black', '1980'),\n", " ('The Dark Side of the Moon', '1973'),\n", " ('The Bodyguard', '1992'),\n", " ('Bat Out of Hell', '1977'),\n", " ('Their Greatest Hits (1971-1975)', '1976'),\n", " ('Saturday Night Fever', '1977'),\n", " ('Rumours', '1977')])\n", "\n", "release_year_dict\n", "```\n", "
\n", "\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ":::{tip}\n", "\n", "การเปลี่ยนข้อมูลชนิด dict ให้เป็นข้อมูล list ของ tuple สามารถเขียนได้หลายวิธีเช่น\n", "\n", "* สร้าง list ใหม่ขึ้นมาโดยใช้ list comprehesion\n", "```\n", "lst_a = [(k, v) for k, v in release_year_dict.items()]\n", "list_a\n", "```\n", "* สร้าง list ใหม่ขึ้นมาโดยใช้ฟังก์ชั่น `list` \n", "```\n", "lst_a = list(release_year_dict.items())\n", "lst_a\n", "```\n", "\n", "โดยเมธอด items() เป็นเมธอดสำหรับดึงทั้งค่า (values) และคีย์ (keys) ทั้งหมดใน Dictionary ออกมา\n", ":::\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**ในกรณีที่ key ที่เป็นสตริง** เราสามารถสร้างตัวแปร Dictionary โดยส่งเป็นคีย์เวิร์ดกิวเมนต์ (keyword arguments) ให้กับฟังก์ชั่น `dict(**kwarg)` ได้\n", "\n", "```python\n", "dic_var = dict(\n", " = ,\n", " = ,\n", " .\n", " .\n", " .\n", " = )\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "จาก Syntex ข้างต้น ลองสร้าง Dictionary ขึ้นมาอีกตัวที่มีข้อมูลเดียวกันโดยใช้ฟังก์ชัน `dict()` แต่ใช้ชื่อตัวแปรใหม่เป็น `dic_b` " ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (4289787341.py, line 3)", "output_type": "error", "traceback": [ "\u001b[0;36m Cell \u001b[0;32mIn [10], line 3\u001b[0;36m\u001b[0m\n\u001b[0;31m dic_b =\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n", "\n", "dic_b = " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "คำตอบคือ" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]:# \"\n", "
Click here for the solution\n", "\n", "```python\n", "dic_b = dict(Thriller='1982',\n", " Back_in_Black='1980',\n", " The_Dark_Side_of_the_Moon='1973',\n", " The_Bodyguard='1992',\n", " Bat_Out_of_Hell='1977',\n", " Their_Greatest_Hits_1971_1975='1976',\n", " Saturday_Night_Fever='1977',\n", " Rumours='1977')\n", "```\n", "\n", "
\n", "\"" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "dic_b = dict(Thriller='1982',\n", " Back_in_Black='1980',\n", " The_Dark_Side_of_the_Moon='1973',\n", " The_Bodyguard='1992',\n", " Bat_Out_of_Hell='1977',\n", " Their_Greatest_Hits_1971_1975='1976',\n", " Saturday_Night_Fever='1977',\n", " Rumours='1977')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Keys\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\qquad$ เราสามารถเข้าถึงสมาชิกเพื่ออ่านค่าหรืออัพเดทข้อมูลได้โดยผ่าน Key \n", "\n", "จากตัวแปร `release_year_dict` ที่เราได้ประกาศไปแล้วกัน เราไม่สามารถใช้ Index เข้าถึงข้อมูลได้ ดังตัวอย่างต่อไปนี้" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "ename": "KeyError", "evalue": "0", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn [12], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# อ่านค่าโดยใช้เลข index\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m \u001b[43mrelease_year_dict\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\n", "\u001b[0;31mKeyError\u001b[0m: 0" ] } ], "source": [ "# อ่านค่าโดยใช้เลข index\n", " \n", "release_year_dict[0] " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เกิดความผิดพลาด _KeyError_ เนื่องจากเลข `0` ไม่ได้ถูกบันทึกเป็น key ใน dict `release_year_dict`" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "'1982'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# อ่านค่าโดยใช้ keys\n", "\n", "release_year_dict['Thriller'] " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ค่าที่แสดงเป็นค่าของข้อมูลนี้สอดคล้องกับคีย์ `Thriller`!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]: # '\n", "[Michael Jackson - Thriller(1982)](https://www.youtube.com/watch?v=sOnqjkJTMaA),\n", "'\n", "![](images/DictsKeyOne.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ในทำนองเดียวกันกับ `The Bodyguard`\n" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "'1992'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# อ่านค่าโดยใช้ keys\n", "\n", "release_year_dict['The Bodyguard'] " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "หรือจะอ่านค่าด้วยเมธอด `get( )` โดยมี Key เป็นอาร์กิวเมนต์ ซึ่งจะได้ผลลัพธ์เหมือนกับการอ่านค่าโดยตรง" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1992'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# อ่านค่าโดยใช้เมธอด get()\n", "\n", "release_year_dict.get('The Bodyguard') " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "แต่ในกรณีของเมธอด `get( )` ถ้า key ที่ส่งเข้าไป ไม่ได้ถูกบันทึกไว้ ไพธอนจะ**ไม่แจ้งข้อผิดพลาด (ไม่เกิด KeyError)** แต่จะส่งค่ากลับเป็น `None`" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "release_year_dict.get('Bad boy') " ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "print(release_year_dict.get('Bad boy'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "กรณีที่ไม่มี key เราสามารถกำหนดให้เมธอด `get` จะส่งค่ากลับเป็นอย่างอื่นที่ไม่ใช่ `None` ก็ได้ เช่น" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1980'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "release_year_dict.get('Back in Black', -1)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "release_year_dict.get('Bad boy', -1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note} 'None'\n", "คนทั่วๆ ไปมักคิดไปว่า None เป็น Keyword แต่จริงๆ แล้ว None เป็นออบเจ็กต์/ข้อมูลชนิดหนึ่งในภาษาไพธอน () มีค่าคล้ายๆ กับค่า null หรือ nil ในภาษาอื่น กล่าวคือ `None` คือ **ค่าที่ไม่มีค่าใดๆ** โปรแกรมเมอร์มักใช้ None เป็นสถานะของ Flag ในการตรวจสอบ รวมถึงใช้เป็นค่าเริ่มต้น (default value) ให้กับตัวแปร (เพื่อให้ตัวแปรตัวนั้นๆ เป็น undefined) หรือค่าส่งกลับ (return value) ของฟังก์ชั่น\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]: # (\n", "\n", ")\n", "\n", "![](images/DictsKeyTwo.png)\n", "\n", "_คลิ๊กฟังเพลง_\n", "* [Whitney Houston - I Will Always Love You (The Bodyguard )](https://www.youtube.com/watch?v=3JWTaaS7LdU)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เราสามารถดึง Keys ทั้งหมดที่มีอยู่ใน Dictionary ออกมาได้โดยใช้เมธอด `keys()`\n" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "dict_keys(['Thriller', 'Back in Black', 'The Dark Side of the Moon', 'The Bodyguard', 'Bat Out of Hell', 'Their Greatest Hits (1971-1975)', 'Saturday Night Fever', 'Rumours'])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ดึง keys ทั้งหมดที่อยู่ใน dictionary\n", "\n", "release_year_dict.keys() " ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(release_year_dict.keys()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]: # \"\n", "
\n", "เมธอด `keys()` ส่งค่ากลับเป็นออบเจ็ค ([view objects](https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects): ) ในรูปของ list ของ Keys ทั้งหมดที่ถูกบันทึกภายใน Dictionary
\n", "\"\n", "\n", "```{note}\n", "เมธอด `keys()` ส่งค่ากลับเป็นออบเจ็ค (([view objects](https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects)): `dict_keys`) ในรูปของ list ของ Keys ทั้งหมดที่ถูกบันทึกภายใน Dictionary\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เปลี่ยนออบเจ็กต์ dict_keys ให้อยู่ในรูปของ List " ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Thriller',\n", " 'Back in Black',\n", " 'The Dark Side of the Moon',\n", " 'The Bodyguard',\n", " 'Bat Out of Hell',\n", " 'Their Greatest Hits (1971-1975)',\n", " 'Saturday Night Fever',\n", " 'Rumours']" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(release_year_dict.keys())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "นอกจากนี้ เรายังสามารถดึง Values ทั้งหมดที่มีอยู่ใน Dictionary ออกมาโดยใช้เมธอด `values()`\n" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "dict_values(['1982', '1980', '1973', '1992', '1977', '1976', '1977', '1977'])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ดึงค่า (values) ทั้งหมดที่มีอยู่ใน dictionary\n", "\n", "release_year_dict.values() " ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(release_year_dict.values()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]: # \"\n", "
\n", "เมธอด `values()` ส่งค่ากลับเป็นออบเจ็ค ([view objects]: ) ในรูปของ list ของ Values ทั้งหมดที่ถูกบันทึกภายใน Dictionary
release_year_dict.items()release_year_dict.items()release_year_dict.items()release_year_dict.items()release_year_dict.items()release_year_dict.items()release_year_dict.items()release_year_dict.items()release_year_dict.items()\n", "\"\n", "\n", "```{note}\n", "เมธอด `values()` ส่งค่ากลับเป็นออบเจ็ค ([view objects]: `dict_values`) ในรูปของ list ของ Values ทั้งหมดที่ถูกบันทึกภายใน Dictionary\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เปลี่ยนให้อยู่ในรูปของ List " ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['1982', '1980', '1973', '1992', '1977', '1976', '1977', '1977']" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(release_year_dict.values())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เราสามารถเพิ่มสมาชิกสมาชิก (adding/inserting) เข้าไปใน Dictionary ได้ง่ายๆ ดังนี้" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "{'Thriller': '1982',\n", " 'Back in Black': '1980',\n", " 'The Dark Side of the Moon': '1973',\n", " 'The Bodyguard': '1992',\n", " 'Bat Out of Hell': '1977',\n", " 'Their Greatest Hits (1971-1975)': '1976',\n", " 'Saturday Night Fever': '1977',\n", " 'Rumours': '1977',\n", " 'Graduation': '2007'}" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# เพิ่มข้อมูลต่อท้ายใน dictionary\n", "\n", "release_year_dict['Graduation'] = '2007'\n", "release_year_dict" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เราสามารถลบ (removing/deleting) สมาชิกได้โดยใช้คำสั่ง `del` หรือ `del()`" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "{'Back in Black': '1980',\n", " 'The Dark Side of the Moon': '1973',\n", " 'The Bodyguard': '1992',\n", " 'Bat Out of Hell': '1977',\n", " 'Their Greatest Hits (1971-1975)': '1976',\n", " 'Saturday Night Fever': '1977',\n", " 'Rumours': '1977',\n", " 'Graduation': '2007'}" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ลบสมาชิกด้วย key\n", "del(release_year_dict['Thriller'])\n", "release_year_dict" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Back in Black': '1980',\n", " 'The Dark Side of the Moon': '1973',\n", " 'The Bodyguard': '1992',\n", " 'Bat Out of Hell': '1977',\n", " 'Their Greatest Hits (1971-1975)': '1976',\n", " 'Saturday Night Fever': '1977',\n", " 'Rumours': '1977'}" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ลบสมาชิกด้วย key\n", "del release_year_dict['Graduation']\n", "release_year_dict" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note}\n", "\n", "`del` เป็น**คำสั่ง (statement)** ไม่ใช่ฟังก์ชัน เป็นคำสั่งลบข้อมูลออกจากหน่วยความจำ จะมีหรือไม่มีวงเล็บก็ได้!\n", "ยกตัวอย่างเช่น ถ้ากำหนดให้ `x, y = 1, 2` คำสั่งต่อไปนี้ไม่ว่าจะเขียนคำสี่งใดก็ตามจะให้ผลลัพธ์เหมือนกัน\n", "\n", "```\n", "del x,y\n", "del (x,y)\n", "del (x),[y]\n", "del [x,(y)]\n", "del ([x], (y))\n", "```\n", "\n", "(Ref: [stackoverflow.com](https://stackoverflow.com/questions/39028249/why-does-del-x-with-parentheses-around-the-variable-name-work)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เราสามารถตรวจสอบว่ามี key ที่ต้องการอยู่ใน Dictionary หรือไม่โดยใช้ตัวดำเนินการ `in`" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ตรวจสอบว่าคีย์อยู่ใน dict หรือไม่\n", "\n", "'The Bodyguard' in release_year_dict" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " หรือเขียนโค้ดแบบนี้ก็ได้" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ตรวจสอบว่าคีย์อยู่ใน dict หรือไม่\n", "\n", "'The Bodyguard' in release_year_dict.keys()" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Thriller' in release_year_dict" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "นอกจากนี้ เรายังสามารถดึงทั้ง Keys และ Values ทั้งหมดที่อยู่ใน Dictionary ออกมาพร้อมกันเป็นคู่ๆ โดยใช้เมธอด `items()`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]: # \"\n", "
\n", "เมธอด `items()` ส่งค่ากลับเป็นออบเจ็ค ([view objects]: `dict_items`) ในรูปของ tuples ที่อยู่ใน list ซึ่งมี key-value pairs มาจาก dictionary
\n", "\"\n", "\n", "```{note}\n", "เมธอด `items()` ส่งค่ากลับเป็นออบเจ็ค ([view objects]: `dict_items`) ในรูปของ tuples ที่อยู่ใน list ซึ่งมี key-value pairs มาจาก dictionary\n", "```" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_items([('Back in Black', '1980'), ('The Dark Side of the Moon', '1973'), ('The Bodyguard', '1992'), ('Bat Out of Hell', '1977'), ('Their Greatest Hits (1971-1975)', '1976'), ('Saturday Night Fever', '1977'), ('Rumours', '1977')])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "release_year_dict.items()" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(release_year_dict.items()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "เปลี่ยนให้อยู่ในรูปของ List" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[('Back in Black', '1980'),\n", " ('The Dark Side of the Moon', '1973'),\n", " ('The Bodyguard', '1992'),\n", " ('Bat Out of Hell', '1977'),\n", " ('Their Greatest Hits (1971-1975)', '1976'),\n", " ('Saturday Night Fever', '1977'),\n", " ('Rumours', '1977')]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(release_year_dict.items())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "สามารถใช้ฟังก์ชัน `len( )` ตรวจสอบจำนวนสมาชิกได้" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(release_year_dict)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**ตารางสรุปเมธอดของ Dictionary (ที่มักใช้บ่อย)**\n", "\n", "| **เมธอด** | **คำอธิบาย** |\n", "| ------------ | ------- |\n", "| clear() | ลบสมาชิกทั้งหมดใน dictionary (กลายเป็น empty dictionary) |\n", "| copy() | ส่งค่ากลับเป็น dict ชุดใหม่ที่มีข้อมูลคัดลอก (clone) จาก dictionary |\n", "| get() | ส่งค่ากลับเป็นค่า (values) ที่สัมพันธ์กับคีย์ (key) ที่ระบุ |\n", "| items() | ส่งค่ากลับเป็นลิสต์ของคู่คีย์และค่า (key-value pair) ทั้งหมดใน dictionary |\n", "| keys() | ส่งค่ากลับเป็นลิสต์ของคีย์ (keys) ทั้งหมดใน dictionary |\n", "| pop() | ลบคีย์ (key) ออกจาก dictionary ถ้ามีคีย์ที่ระบุอยู่ และคืนค่ากลับเป็นค่า (value) ที่สัมพันธ์กับคีย์ที่ลบ|\n", "| popitem() | ลบคู่คีย์และค่า (key-value pair) ตัวสุดท้ายออกจาก dictionary|\n", "| update() | รวมข้อมูล (merge) ใน dictionary กับ dictionary อื่นเข้าด้วยกัน |\n", "| values() | ส่งค่ากลับเป็นลิสต์ของค่า (values) ทั้งหมดใน dictionary |\n", "\n", "Ref: https://realpython.com/python-dicts/#built-in-dictionary-methods\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "## [Exercise]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`1.` จงตอบคำถามจงตอบคำถามจาก dictionary ต่อไปนี้\n" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'The Bodyguard': '1992',\n", " 'Saturday Night Fever': '1977',\n", " 'My Heart Will Go On': '1997',\n", " 'Happy': '2013',\n", " 'A Real Hero': '2011'}" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "soundtrack_dic = {\"The Bodyguard\":\"1992\", \"Saturday Night Fever\":\"1977\", \"My Heart Will Go On\": \"1997\", \"Happy\": \"2013\", \"A Real Hero\": \"2011\"}\n", "soundtrack_dic " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1.1) จงแสดงค่า Keys ทั้งหมดที่อยู่ใน Dictionary `soundtrack_dic` ในรูปของ list\n" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]: # \"\n", "
Click here for the solution\n", "\n", "```python\n", "soundtrack_dic.keys() # Output: ['The Bodyguard', 'Saturday Night Fever', 'My Heart Will Go On', 'Happy', 'A Real Hero'] \n", "\n", "```\n", "\n", "
\n", "\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1.2) จงแสดงค่า Values ทั้งหมดที่อยู่ใน Dictionary `soundtrack_dic` ในรูปของ list\n" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]:# \"\n", "
Click here for the solution\n", "\n", "```python\n", "soundtrack_dic.values() # Outpit: ['1992', '1977', '1997', '2013', '2011']\n", "\n", "```\n", "\n", "
\n", "\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`2` กำหนดให้ อัลบั้ม Back in Black, The Bodyguard และ Thriller มียอดขายอย่างเป็นทางการ 50, 50 และ 65 ล้านเหรียญสหรัฐ ตามลำดับ" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2.1) จงสร้างตัวแปร dictionary `album_sales_dict` โดยให้ keys เป็นชื่ออัลบัมและยอดขายเป็น vaules หน่วยล้านเหรียญสหรัฐ\n" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]: # \"\n", "
Click here for the solution\n", "\n", "```python\n", "album_sales_dict = {'The Bodyguard':50, 'Back in Black':50, 'Thriller':65}\n", "```\n", " หรือ\n", " \n", "```python\n", "album_sales_dict = dict([('The Bodyguard', 50), ('Back in Black', 50), ('Thriller', 65)])\n", "```\n", " หรือ\n", " \n", "```python\n", "album_sales_dict = dict((('The Bodyguard', 50), ('Back in Black', 50), ('Thriller', 65)))\n", "```\n", "\n", "
\n", "\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2.2) ใช้ key หายอดขายของอัลบัม Thriller\n" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]: # (\n", "
Click here for the solution\n", "\n", "```python\n", "album_sales_dict[\"Thriller\"]\n", "\n", "```\n", "
\n", ")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2.3) ใช้เมธอด `keys()` ดึงชื่ออัลบัมที่อยู่ใน dictionary ออกมาในรูปของ list\n" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]: # \"\n", "
Click here for the solution\n", "\n", "```python\n", "album_sales_dict.keys()\n", "\n", "```\n", "
\n", "\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2.4) ใช้เมธอด `values` ดึง Value ที่เป็นยอดขายใน dictionary ออกมาในรูปของ list" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]: # \"\n", "
Click here for the solution\n", "\n", "```python\n", "album_sales_dict.values()\n", "\n", "```\n", "
\n", "\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`3` จงสร้าง dictionary `person` ที่มีข้อมูล\n", "\n", "> ```(python)\n", "> {'fname': 'แฮ่งดี',\n", "> 'lname': 'เก่งขยันอดทน',\n", "> 'age': 21,\n", "> 'spouse': 'งาม',\n", "> 'children': ['พอดี', 'พอใจ', 'เพรียงพอ'],\n", "> 'pets': {'dog': 'ชิโระ', 'cat': 'คุโรเนโกะ'},\n", "> 'occup': ('ครู', 'ชาวสวน')}\n", "> ```\n", "\n", "โดยทำตามขั้นตอนต่อไปนี้" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3.1) สร้างตัวแปร `person` เป็นข้อมูล dictionary ว่างๆ (empty dict)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3.2) โค้ดตรวจสอบว่าตัวแปร `person` เป็นชนิด `dict` หรือไม่ " ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3.3) คำสั่งกำหนด key `fname` (ชื่อ)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3.4) คำสั่งกำหนด key `lname` (นามสกุล)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3.5) คำสั่งกำหนด key `age` (อายุ)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3.6) คำสั่งกำหนด key `spouse` (คู่สมรส)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3.7) คำสั่งกำหนด key `children` (บุตรธิดา)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3.8) คำสั่งกำหนด key `pets` (สัตว์เลี้ยง)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3.9) คำสั่งกำหนด key `occup` (อาชีพ)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3.10) คำสั่งแสดงชื่อของลูกคนสุดท้าย" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3.11) คำสั่งแสดงชื่อของแมว" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3.12) คำสั่งแสดงอาชีพว่ามีกี่อาชีพ" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "# เขียนโค้ดด้านล่าง แล้วกด Shift+Enter\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[//]: # \"\n", "
Click here for the solution\n", "\n", "```python\n", "\n", "person = {}\n", "type(person) == dict\n", "person['fname'] = 'แฮ่งดี'\n", "person['lname'] = 'เก่งขยันอดทน'\n", "person['age'] = 21\n", "person['spouse'] = 'งาม'\n", "person['children'] = ['พอดี', 'พอใจ', 'เพรียงพอ']\n", "person['pets'] = {'dog': 'ชิโระ', 'cat': 'คุโรเนโกะ'}\n", "person['occup'] = ('ครู','ชาวสวน')\n", "\n", "person['children'][-1]\n", "person['pets']['cat']\n", "len(person['occup'])\n", "```\n", "
\n", "\"\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "“Make it work, make it right, make it fast.” Kent Beck (an American software engineer and the creator of the Extreme Programming and Test Driven Development software development ...)" ] }, { "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": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }