diff --git a/03-dict-set/03-dict-set.ipynb b/03-dict-set/03-dict-set.ipynb index 1bfc9d5..556a63f 100644 --- a/03-dict-set/03-dict-set.ipynb +++ b/03-dict-set/03-dict-set.ipynb @@ -1,28 +1,47 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, + "source": [ + "# Chapter 3 — Dictionaries and Sets" + ] + }, { "cell_type": "code", "execution_count": 1, - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27) \n", - "[Clang 6.0 (clang-600.0.57)]\n" + "3.10.4 (main, Mar 31 2022, 08:41:55) [GCC 7.5.0]\n" ] } ], "source": [ "import sys\n", + "\n", "print(sys.version)" ] }, { "cell_type": "code", "execution_count": 2, - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { @@ -37,6 +56,7 @@ ], "source": [ "from collections import abc\n", + "\n", "my_dict = {}\n", "isinstance(my_dict, abc.Mapping)" ] @@ -44,7 +64,11 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { @@ -64,7 +88,11 @@ { "cell_type": "code", "execution_count": 4, - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { @@ -84,8 +112,12 @@ }, { "cell_type": "code", - "execution_count": 7, - "metadata": {}, + "execution_count": 5, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "name": "stdout", @@ -105,8 +137,12 @@ }, { "cell_type": "code", - "execution_count": 5, - "metadata": {}, + "execution_count": 6, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { @@ -114,7 +150,7 @@ "5149391500123939311" ] }, - "execution_count": 5, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -126,8 +162,12 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": {}, + "execution_count": 7, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { @@ -135,7 +175,7 @@ "True" ] }, - "execution_count": 8, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -151,8 +191,12 @@ }, { "cell_type": "code", - "execution_count": 10, - "metadata": {}, + "execution_count": 8, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { @@ -160,7 +204,7 @@ "{'one': 1, 'two': 2, 'three': 3}" ] }, - "execution_count": 10, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -171,8 +215,12 @@ }, { "cell_type": "code", - "execution_count": 11, - "metadata": {}, + "execution_count": 9, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { @@ -180,7 +228,7 @@ "['one', 'two', 'three']" ] }, - "execution_count": 11, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -191,8 +239,12 @@ }, { "cell_type": "code", - "execution_count": 12, - "metadata": {}, + "execution_count": 10, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { @@ -200,7 +252,7 @@ "{'two': 2, 'one': 1, 'three': 3}" ] }, - "execution_count": 12, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -211,8 +263,12 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": {}, + "execution_count": 11, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { @@ -220,7 +276,7 @@ "('three', 3)" ] }, - "execution_count": 13, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -231,8 +287,12 @@ }, { "cell_type": "code", - "execution_count": 14, - "metadata": {}, + "execution_count": 12, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { @@ -240,7 +300,7 @@ "{'two': 2, 'one': 1}" ] }, - "execution_count": 14, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -249,30 +309,60 @@ "c" ] }, + { + "cell_type": "markdown", + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, + "source": [ + "## Modern dict Syntax" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, + "source": [ + "#### Example 3-1. Examples of dict comprehensions" + ] + }, { "cell_type": "code", - "execution_count": 15, - "metadata": {}, + "execution_count": 13, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [], "source": [ - "dial_codes = [ # <1>\n", + "dial_codes = [ # <1>\n", " (880, 'Bangladesh'),\n", - " (55, 'Brazil'),\n", - " (86, 'China'),\n", - " (91, 'India'),\n", - " (62, 'Indonesia'),\n", - " (81, 'Japan'),\n", + " (55, 'Brazil'),\n", + " (86, 'China'),\n", + " (91, 'India'),\n", + " (62, 'Indonesia'),\n", + " (81, 'Japan'),\n", " (234, 'Nigeria'),\n", - " (92, 'Pakistan'),\n", - " (7, 'Russia'),\n", - " (1, 'United States'),\n", + " (92, 'Pakistan'),\n", + " (7, 'Russia'),\n", + " (1, 'United States'),\n", "]" ] }, { "cell_type": "code", - "execution_count": 17, - "metadata": {}, + "execution_count": 14, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { @@ -289,20 +379,24 @@ " 'United States': 1}" ] }, - "execution_count": 17, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "country_dial = {country: code for code, country in dial_codes}\n", + "country_dial = {country: code for code, country in dial_codes} # <2>\n", "country_dial" ] }, { "cell_type": "code", - "execution_count": 18, - "metadata": {}, + "execution_count": 15, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { @@ -310,55 +404,1544 @@ "{55: 'BRAZIL', 62: 'INDONESIA', 7: 'RUSSIA', 1: 'UNITED STATES'}" ] }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "{code: country.upper() # <3>\n", + " for country, code in sorted(country_dial.items())\n", + " if code < 70}" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'x': 1, 'y': 2, 'z': 3}" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def dump(**kwargs):\n", + " return kwargs\n", + "\n", + "\n", + "dump(**{'x': 1}, y=2, **{'z': 3})" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'a': 2, 'b': 4, 'c': 6}" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d1 = {'a': 1, 'b': 3}\n", + "d2 = {'a': 2, 'b': 4, 'c': 6}\n", + "{'a': 2, 'b': 4, 'c': 6}" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'a': 1, 'b': 3}" + ] + }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "{code: country.upper() \n", - " for country, code in sorted(country_dial.items())\n", - " if code < 70}" + "d1" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'a': 2, 'b': 4, 'c': 6}" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d1 |= d2\n", + "d1" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, + "source": [ + "## Pattern Matching with Mappings" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, + "source": [ + "#### Example 3-2. `creator.py`: `get_creators()` extracts names of creators from media records" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, + "source": [ + "**Note**: Python 3.10 is required" ] }, { "cell_type": "code", "execution_count": 20, - "metadata": {}, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [], + "source": [ + "def get_creators(record: dict) -> list:\n", + " match record:\n", + " case {'type': 'book', 'api': 2, 'authors': [*names]}: # <1>\n", + " return names\n", + " case {'type': 'book', 'api': 1, 'author': name}: # <2>\n", + " return [name]\n", + " case {'type': 'book'}: # <3>\n", + " raise ValueError(f\"Invalid 'book' record: {record!r}\")\n", + " case {'type': 'movie', 'director': name}: # <4>\n", + " return [name]\n", + " case _: # <5>\n", + " raise ValueError(f'Invalid record: {record!r}')" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [ { "data": { "text/plain": [ - "{'Pakistan': 92,\n", - " 'Indonesia': 62,\n", - " 'Russia': 7,\n", - " 'Japan': 81,\n", - " 'United States': 1,\n", - " 'China': 86,\n", - " 'Brazil': 55,\n", - " 'Bangladesh': 880,\n", - " 'Nigeria': 234,\n", - " 'India': 91}" + "['Douglas Hofstadter']" ] }, - "execution_count": 20, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "from random import shuffle\n", - "shuffle(dial_codes)\n", - "country_dial = {country: code for code, country in dial_codes}\n", - "country_dial" + "b1 = dict(api=1, author='Douglas Hofstadter',\n", + " type='book', title='Gödel, Escher, Bach')\n", + "get_creators(b1)" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 22, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [], - "source": [] + "source": [ + "from collections import OrderedDict\n", + "\n", + "b2 = OrderedDict(api=2, type='book',\n", + " title='Python in a Nutshell',\n", + " authors='Martelli Ravenscroft Holden'.split())" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['Martelli', 'Ravenscroft', 'Holden']" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "get_creators(b2)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "pycharm": { + "name": "#%%\n" + }, + "tags": [ + "raises-exception" + ] + }, + "outputs": [ + { + "ename": "ValueError", + "evalue": "Invalid 'book' record: {'type': 'book', 'pages': 770}", + "output_type": "error", + "traceback": [ + "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", + "\u001B[0;31mValueError\u001B[0m Traceback (most recent call last)", + "Input \u001B[0;32mIn [24]\u001B[0m, in \u001B[0;36m\u001B[0;34m()\u001B[0m\n\u001B[0;32m----> 1\u001B[0m \u001B[43mget_creators\u001B[49m\u001B[43m(\u001B[49m\u001B[43m{\u001B[49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[38;5;124;43mtype\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[43m:\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[38;5;124;43mbook\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[38;5;124;43mpages\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[43m:\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m770\u001B[39;49m\u001B[43m}\u001B[49m\u001B[43m)\u001B[49m\n", + "Input \u001B[0;32mIn [20]\u001B[0m, in \u001B[0;36mget_creators\u001B[0;34m(record)\u001B[0m\n\u001B[1;32m 1\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mget_creators\u001B[39m(record: \u001B[38;5;28mdict\u001B[39m) \u001B[38;5;241m-\u001B[39m\u001B[38;5;241m>\u001B[39m \u001B[38;5;28mlist\u001B[39m:\n\u001B[1;32m 2\u001B[0m \u001B[38;5;28;01mmatch\u001B[39;00m record:\n\u001B[1;32m 3\u001B[0m \u001B[38;5;28;01mcase\u001B[39;00m {\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mtype\u001B[39m\u001B[38;5;124m'\u001B[39m: \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mbook\u001B[39m\u001B[38;5;124m'\u001B[39m, \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mapi\u001B[39m\u001B[38;5;124m'\u001B[39m: \u001B[38;5;241m2\u001B[39m, \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mauthors\u001B[39m\u001B[38;5;124m'\u001B[39m: [\u001B[38;5;241m*\u001B[39mnames]}: \u001B[38;5;66;03m# <1>\u001B[39;00m\n\u001B[1;32m 4\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m names\n\u001B[1;32m 5\u001B[0m \u001B[38;5;28;01mcase\u001B[39;00m {\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mtype\u001B[39m\u001B[38;5;124m'\u001B[39m: \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mbook\u001B[39m\u001B[38;5;124m'\u001B[39m, \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mapi\u001B[39m\u001B[38;5;124m'\u001B[39m: \u001B[38;5;241m1\u001B[39m, \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mauthor\u001B[39m\u001B[38;5;124m'\u001B[39m: name}: \u001B[38;5;66;03m# <2>\u001B[39;00m\n\u001B[1;32m 6\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m [name]\n\u001B[1;32m 7\u001B[0m \u001B[38;5;28;01mcase\u001B[39;00m {\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mtype\u001B[39m\u001B[38;5;124m'\u001B[39m: \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mbook\u001B[39m\u001B[38;5;124m'\u001B[39m}: \u001B[38;5;66;03m# <3>\u001B[39;00m\n\u001B[0;32m----> 8\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(\u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mInvalid \u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mbook\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m record: \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mrecord\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[38;5;124m\"\u001B[39m)\n\u001B[1;32m 9\u001B[0m \u001B[38;5;28;01mcase\u001B[39;00m {\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mtype\u001B[39m\u001B[38;5;124m'\u001B[39m: \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mmovie\u001B[39m\u001B[38;5;124m'\u001B[39m, \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mdirector\u001B[39m\u001B[38;5;124m'\u001B[39m: name}: \u001B[38;5;66;03m# <4>\u001B[39;00m\n\u001B[1;32m 10\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m [name]\n\u001B[1;32m 11\u001B[0m \u001B[38;5;28;01mcase\u001B[39;00m \u001B[38;5;28;01m_\u001B[39;00m: \u001B[38;5;66;03m# <5>\u001B[39;00m\n\u001B[1;32m 12\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(\u001B[38;5;124mf\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mInvalid record: \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mrecord\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[38;5;124m'\u001B[39m)\n", + "\u001B[0;31mValueError\u001B[0m: Invalid 'book' record: {'type': 'book', 'pages': 770}" + ] + } + ], + "source": [ + "get_creators({'type': 'book', 'pages': 770})" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "pycharm": { + "name": "#%%\n" + }, + "tags": [ + "raises-exception" + ] + }, + "outputs": [ + { + "ename": "ValueError", + "evalue": "Invalid record: 'Spam, spam, spam'", + "output_type": "error", + "traceback": [ + "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", + "\u001B[0;31mValueError\u001B[0m Traceback (most recent call last)", + "Input \u001B[0;32mIn [25]\u001B[0m, in \u001B[0;36m\u001B[0;34m()\u001B[0m\n\u001B[0;32m----> 1\u001B[0m \u001B[43mget_creators\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[38;5;124;43mSpam, spam, spam\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[43m)\u001B[49m\n", + "Input \u001B[0;32mIn [20]\u001B[0m, in \u001B[0;36mget_creators\u001B[0;34m(record)\u001B[0m\n\u001B[1;32m 1\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mget_creators\u001B[39m(record: \u001B[38;5;28mdict\u001B[39m) \u001B[38;5;241m-\u001B[39m\u001B[38;5;241m>\u001B[39m \u001B[38;5;28mlist\u001B[39m:\n\u001B[1;32m 2\u001B[0m \u001B[38;5;28;01mmatch\u001B[39;00m record:\n\u001B[1;32m 3\u001B[0m \u001B[38;5;28;01mcase\u001B[39;00m {\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mtype\u001B[39m\u001B[38;5;124m'\u001B[39m: \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mbook\u001B[39m\u001B[38;5;124m'\u001B[39m, \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mapi\u001B[39m\u001B[38;5;124m'\u001B[39m: \u001B[38;5;241m2\u001B[39m, \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mauthors\u001B[39m\u001B[38;5;124m'\u001B[39m: [\u001B[38;5;241m*\u001B[39mnames]}: \u001B[38;5;66;03m# <1>\u001B[39;00m\n\u001B[1;32m 4\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m names\n\u001B[1;32m 5\u001B[0m \u001B[38;5;28;01mcase\u001B[39;00m {\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mtype\u001B[39m\u001B[38;5;124m'\u001B[39m: \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mbook\u001B[39m\u001B[38;5;124m'\u001B[39m, \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mapi\u001B[39m\u001B[38;5;124m'\u001B[39m: \u001B[38;5;241m1\u001B[39m, \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mauthor\u001B[39m\u001B[38;5;124m'\u001B[39m: name}: \u001B[38;5;66;03m# <2>\u001B[39;00m\n\u001B[1;32m 6\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m [name]\n\u001B[1;32m 7\u001B[0m \u001B[38;5;28;01mcase\u001B[39;00m {\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mtype\u001B[39m\u001B[38;5;124m'\u001B[39m: \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mbook\u001B[39m\u001B[38;5;124m'\u001B[39m}: \u001B[38;5;66;03m# <3>\u001B[39;00m\n\u001B[1;32m 8\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(\u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mInvalid \u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mbook\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m record: \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mrecord\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[38;5;124m\"\u001B[39m)\n\u001B[1;32m 9\u001B[0m \u001B[38;5;28;01mcase\u001B[39;00m {\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mtype\u001B[39m\u001B[38;5;124m'\u001B[39m: \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mmovie\u001B[39m\u001B[38;5;124m'\u001B[39m, \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mdirector\u001B[39m\u001B[38;5;124m'\u001B[39m: name}: \u001B[38;5;66;03m# <4>\u001B[39;00m\n\u001B[1;32m 10\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m [name]\n\u001B[1;32m 11\u001B[0m \u001B[38;5;28;01mcase\u001B[39;00m \u001B[38;5;28;01m_\u001B[39;00m: \u001B[38;5;66;03m# <5>\u001B[39;00m\n\u001B[0;32m---> 12\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(\u001B[38;5;124mf\u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mInvalid record: \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mrecord\u001B[38;5;132;01m!r}\u001B[39;00m\u001B[38;5;124m'\u001B[39m)\n", + "\u001B[0;31mValueError\u001B[0m: Invalid record: 'Spam, spam, spam'" + ] + } + ], + "source": [ + "get_creators('Spam, spam, spam')" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ice cream details: {'flavor': 'vanilla', 'cost': 199}\n" + ] + } + ], + "source": [ + "food = dict(category='ice cream', flavor='vanilla', cost=199)\n", + "match food:\n", + " case {'category': 'ice cream', **details}:\n", + " print(f'Ice cream details: {details}')" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, + "source": [ + "## Standard API of Mapping Types" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### What is Hashable" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_dict = {}\n", + "isinstance(my_dict, abc.Mapping)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "isinstance(my_dict, abc.MutableMapping)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "-3907003130834322577" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tt = (1, 2, (30, 40))\n", + "hash(tt)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "pycharm": { + "name": "#%%\n" + }, + "tags": [ + "raises-exception" + ] + }, + "outputs": [ + { + "ename": "TypeError", + "evalue": "unhashable type: 'list'", + "output_type": "error", + "traceback": [ + "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", + "\u001B[0;31mTypeError\u001B[0m Traceback (most recent call last)", + "Input \u001B[0;32mIn [30]\u001B[0m, in \u001B[0;36m\u001B[0;34m()\u001B[0m\n\u001B[1;32m 1\u001B[0m tl \u001B[38;5;241m=\u001B[39m (\u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m2\u001B[39m, [\u001B[38;5;241m30\u001B[39m, \u001B[38;5;241m40\u001B[39m])\n\u001B[0;32m----> 2\u001B[0m \u001B[38;5;28;43mhash\u001B[39;49m\u001B[43m(\u001B[49m\u001B[43mtl\u001B[49m\u001B[43m)\u001B[49m\n", + "\u001B[0;31mTypeError\u001B[0m: unhashable type: 'list'" + ] + } + ], + "source": [ + "tl = (1, 2, [30, 40])\n", + "hash(tl)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5149391500123939311" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tf = (1, 2, frozenset([30, 40]))\n", + "hash(tf)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, + "source": [ + "### Inserting or Updating Mutable Values" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Example 3-4. [`index0.py`](index0.py): Processing the \"Zen of Python\"; each line shows a word and a list of occurences coded as pairs" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "pycharm": { + "name": "#%%\n" + }, + "scrolled": true + }, + "outputs": [], + "source": [ + "# !python3 index0.py zen.txt" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, + "source": [ + "#### Example 3-5. [`index.py`](index.py) uses `dict.setdefault` to fetch and update a list of word occurrences from the index in a single line; contrast with Example 3-4." + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "# python3 index.py zen.txt" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Automatic handling of Missing Keys" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `defaultdict`: Another take on missing keys" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Example 3-6. [`index_default.py`](index_default.py): using defaultdict instead of the setdefault method" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "scrolled": true, + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [], + "source": [ + "# !python index_default.py zen.txt" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### The `__missing__` method" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Example 3-7. When searching for a nonstring key, [`StrKeyDict0`](strkeydict0.py) converts it to `str` when it is not found" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'two'" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from strkeydict0 import StrKeyDict0\n", + "\n", + "d = StrKeyDict0([('2', 'two'), ('4', 'four')])\n", + "d['2']" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'four'" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d[4]" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "tags": [ + "raises-exception" + ] + }, + "outputs": [ + { + "ename": "KeyError", + "evalue": "'1'", + "output_type": "error", + "traceback": [ + "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", + "\u001B[0;31mKeyError\u001B[0m Traceback (most recent call last)", + "Input \u001B[0;32mIn [37]\u001B[0m, in \u001B[0;36m\u001B[0;34m()\u001B[0m\n\u001B[0;32m----> 1\u001B[0m \u001B[43md\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;241;43m1\u001B[39;49m\u001B[43m]\u001B[49m\n", + "File \u001B[0;32m~/git/training/example-code-2e/03-dict-set/strkeydict0.py:44\u001B[0m, in \u001B[0;36mStrKeyDict0.__missing__\u001B[0;34m(self, key)\u001B[0m\n\u001B[1;32m 42\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28misinstance\u001B[39m(key, \u001B[38;5;28mstr\u001B[39m): \u001B[38;5;66;03m# <2>\u001B[39;00m\n\u001B[1;32m 43\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mKeyError\u001B[39;00m(key)\n\u001B[0;32m---> 44\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28;43mself\u001B[39;49m\u001B[43m[\u001B[49m\u001B[38;5;28;43mstr\u001B[39;49m\u001B[43m(\u001B[49m\u001B[43mkey\u001B[49m\u001B[43m)\u001B[49m\u001B[43m]\u001B[49m\n", + "File \u001B[0;32m~/git/training/example-code-2e/03-dict-set/strkeydict0.py:43\u001B[0m, in \u001B[0;36mStrKeyDict0.__missing__\u001B[0;34m(self, key)\u001B[0m\n\u001B[1;32m 41\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21m__missing__\u001B[39m(\u001B[38;5;28mself\u001B[39m, key):\n\u001B[1;32m 42\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28misinstance\u001B[39m(key, \u001B[38;5;28mstr\u001B[39m): \u001B[38;5;66;03m# <2>\u001B[39;00m\n\u001B[0;32m---> 43\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mKeyError\u001B[39;00m(key)\n\u001B[1;32m 44\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mself\u001B[39m[\u001B[38;5;28mstr\u001B[39m(key)]\n", + "\u001B[0;31mKeyError\u001B[0m: '1'" + ] + } + ], + "source": [ + "d[1]" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'two'" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d.get('2')" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'four'" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d.get(4)" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'N/A'" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d.get(1, 'N/A')" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2 in d" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1 in d" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Variations of `dict`" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `collections.ChainMap`" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d1 = dict(a=1, b=3)\n", + "d2 = dict(a=2, b=4, c=6)\n", + "from collections import ChainMap\n", + "chain = ChainMap(d1, d2)\n", + "chain['a']" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "chain['c']" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'a': 1, 'b': 3, 'c': -1}" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "chain['c'] = -1\n", + "d1" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'a': 2, 'b': 4, 'c': 6}" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### `collections.Counter`" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Counter({'a': 5, 'b': 2, 'r': 2, 'c': 1, 'd': 1})" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import collections\n", + "ct = collections.Counter('abracadabra')\n", + "ct" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Counter({'a': 10, 'b': 2, 'r': 2, 'c': 1, 'd': 1, 'z': 3})" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ct.update('aaaaazzz')\n", + "ct" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('a', 10), ('z', 3), ('b', 2)]" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ct.most_common(3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Immutable Mappings" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Example 3-10. `MappingProxyType` builds a read-only `mappingproxy` instance from a `dict`" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "mappingproxy({1: 'A'})" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from types import MappingProxyType\n", + "d = {1: 'A'}\n", + "d_proxy = MappingProxyType(d)\n", + "d_proxy" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'A'" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d_proxy[1]" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": { + "tags": [ + "raises-exception" + ] + }, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'mappingproxy' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", + "\u001B[0;31mTypeError\u001B[0m Traceback (most recent call last)", + "Input \u001B[0;32mIn [52]\u001B[0m, in \u001B[0;36m\u001B[0;34m()\u001B[0m\n\u001B[0;32m----> 1\u001B[0m d_proxy[\u001B[38;5;241m2\u001B[39m] \u001B[38;5;241m=\u001B[39m \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mx\u001B[39m\u001B[38;5;124m'\u001B[39m\n", + "\u001B[0;31mTypeError\u001B[0m: 'mappingproxy' object does not support item assignment" + ] + } + ], + "source": [ + "d_proxy[2] = 'x'" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "mappingproxy({1: 'A', 2: 'B'})" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d[2] = 'B'\n", + "d_proxy" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'B'" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d_proxy[2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Dictionary Views" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Example 3-11. The `.values()` method returns a view of the values in a `dict`" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_values([10, 20, 30])" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d = dict(a=10, b=20, c=30)\n", + "values = d.values()\n", + "values" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(values)" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[10, 20, 30]" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(values)" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "reversed(values)" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": { + "tags": [ + "raises-exception" + ] + }, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'dict_values' object is not subscriptable", + "output_type": "error", + "traceback": [ + "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", + "\u001B[0;31mTypeError\u001B[0m Traceback (most recent call last)", + "Input \u001B[0;32mIn [59]\u001B[0m, in \u001B[0;36m\u001B[0;34m()\u001B[0m\n\u001B[0;32m----> 1\u001B[0m \u001B[43mvalues\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;241;43m0\u001B[39;49m\u001B[43m]\u001B[49m\n", + "\u001B[0;31mTypeError\u001B[0m: 'dict_values' object is not subscriptable" + ] + } + ], + "source": [ + "values[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'a': 10, 'b': 20, 'c': 30, 'z': 99}" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d['z'] = 99\n", + "d" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_values([10, 20, 30, 99])" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "values" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": { + "tags": [ + "raises-exception" + ] + }, + "outputs": [ + { + "ename": "TypeError", + "evalue": "cannot create 'dict_values' instances", + "output_type": "error", + "traceback": [ + "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", + "\u001B[0;31mTypeError\u001B[0m Traceback (most recent call last)", + "Input \u001B[0;32mIn [62]\u001B[0m, in \u001B[0;36m\u001B[0;34m()\u001B[0m\n\u001B[1;32m 1\u001B[0m values_class \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mtype\u001B[39m({}\u001B[38;5;241m.\u001B[39mvalues())\n\u001B[0;32m----> 2\u001B[0m v \u001B[38;5;241m=\u001B[39m \u001B[43mvalues_class\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\n", + "\u001B[0;31mTypeError\u001B[0m: cannot create 'dict_values' instances" + ] + } + ], + "source": [ + "values_class = type({}.values())\n", + "v = values_class()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Set Theory" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'bacon', 'eggs', 'spam'}" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l = ['spam', 'spam', 'eggs', 'spam', 'bacon', 'eggs']\n", + "set(l)" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['bacon', 'spam', 'eggs']" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(set(l))" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['spam', 'eggs', 'bacon'])" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Preserve the order of the first occurence\n", + "dict.fromkeys(l).keys()" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['spam', 'eggs', 'bacon']" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(dict.fromkeys(l).keys())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Set Literals" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = {1}\n", + "type(s)" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{1}" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 69, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.pop()" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set()" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "frozenset(range(10))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Set Comprehensions" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Example 3-15. Build a set of Latin-1 characters that have the word “SIGN” in their Unicode names" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'#',\n", + " '$',\n", + " '%',\n", + " '+',\n", + " '<',\n", + " '=',\n", + " '>',\n", + " '¢',\n", + " '£',\n", + " '¤',\n", + " '¥',\n", + " '§',\n", + " '©',\n", + " '¬',\n", + " '®',\n", + " '°',\n", + " '±',\n", + " 'µ',\n", + " '¶',\n", + " '×',\n", + " '÷'}" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from unicodedata import name\n", + "{chr(i) for i in range(32, 256) if 'SIGN' in name(chr(i),'')}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Set Operations on dict Views" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'b', 'd'}" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d1 = dict(a=1, b=2, c=3, d=4)\n", + "d2 = dict(b=20, d=40, e=50)\n", + "d1.keys() & d2.keys()" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'a'}" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = {'a', 'e', 'i'}\n", + "d1.keys() & s" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'a', 'b', 'c', 'd', 'e', 'i'}" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d1.keys() | s" + ] } ], "metadata": { @@ -377,9 +1960,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.10.4" } }, "nbformat": 4, "nbformat_minor": 2 -} +} \ No newline at end of file diff --git a/03-dict-set/README.md b/03-dict-set/README.md index 6a059a1..fc29774 100644 --- a/03-dict-set/README.md +++ b/03-dict-set/README.md @@ -8,7 +8,7 @@ Sample code for Chapter 3 of _Fluent Python 2e_ by Luciano Ramalho (O'Reilly, 20 Use Python's standard ``doctest`` module, for example: - $ python3 -m doctest bisect_demo.py -v + $ python3 -m doctest creator.py -v ### Jupyter Notebook