ch03: wip
This commit is contained in:
parent
8a1b6fe145
commit
59818e0500
385
03-dict-set/03-dict-set.ipynb
Normal file
385
03-dict-set/03-dict-set.ipynb
Normal file
@ -0,0 +1,385 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"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"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"print(sys.version)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"True"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from collections import abc\n",
|
||||
"my_dict = {}\n",
|
||||
"isinstance(my_dict, abc.Mapping)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"True"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"isinstance(my_dict, abc.MutableMapping)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"-3907003130834322577"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"tt = (1, 2, (30, 40))\n",
|
||||
"hash(tt)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"unhashable type: 'list'\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"tl = (1, 2, [30, 40])\n",
|
||||
"try:\n",
|
||||
" hash(tl)\n",
|
||||
"except TypeError as e:\n",
|
||||
" print(e)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"5149391500123939311"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"tf = (1, 2, frozenset([30, 40]))\n",
|
||||
"hash(tf)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"True"
|
||||
]
|
||||
},
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"a = dict(one=1, two=2, three=3)\n",
|
||||
"b = {'three': 3, 'two': 2, 'one': 1}\n",
|
||||
"c = dict([('two', 2), ('one', 1), ('three', 3)])\n",
|
||||
"d = dict(zip(['one', 'two', 'three'], [1, 2, 3]))\n",
|
||||
"e = dict({'three': 3, 'one': 1, 'two': 2})\n",
|
||||
"a == b == c == d == e"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'one': 1, 'two': 2, 'three': 3}"
|
||||
]
|
||||
},
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"a"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['one', 'two', 'three']"
|
||||
]
|
||||
},
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"list(a.keys())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'two': 2, 'one': 1, 'three': 3}"
|
||||
]
|
||||
},
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"c"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"('three', 3)"
|
||||
]
|
||||
},
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"c.popitem()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'two': 2, 'one': 1}"
|
||||
]
|
||||
},
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"c"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dial_codes = [ # <1>\n",
|
||||
" (880, 'Bangladesh'),\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",
|
||||
"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'Bangladesh': 880,\n",
|
||||
" 'Brazil': 55,\n",
|
||||
" 'China': 86,\n",
|
||||
" 'India': 91,\n",
|
||||
" 'Indonesia': 62,\n",
|
||||
" 'Japan': 81,\n",
|
||||
" 'Nigeria': 234,\n",
|
||||
" 'Pakistan': 92,\n",
|
||||
" 'Russia': 7,\n",
|
||||
" 'United States': 1}"
|
||||
]
|
||||
},
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"country_dial = {country: code for code, country in dial_codes}\n",
|
||||
"country_dial"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{55: 'BRAZIL', 62: 'INDONESIA', 7: 'RUSSIA', 1: 'UNITED STATES'}"
|
||||
]
|
||||
},
|
||||
"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}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"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}"
|
||||
]
|
||||
},
|
||||
"execution_count": 20,
|
||||
"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"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
21
03-dict-set/README.md
Normal file
21
03-dict-set/README.md
Normal file
@ -0,0 +1,21 @@
|
||||
# Dictionaries and Sets
|
||||
|
||||
Sample code for Chapter 3 of _Fluent Python 2e_ by Luciano Ramalho (O'Reilly, 2020)
|
||||
|
||||
## Running the tests
|
||||
|
||||
### Doctests
|
||||
|
||||
Use Python's standard ``doctest`` module, for example:
|
||||
|
||||
$ python3 -m doctest bisect_demo.py -v
|
||||
|
||||
### Jupyter Notebook
|
||||
|
||||
Install ``pytest`` and the ``nbval`` plugin:
|
||||
|
||||
$ pip install pytest nbval
|
||||
|
||||
Run:
|
||||
|
||||
$ pytest --nbval
|
@ -1,4 +0,0 @@
|
||||
Sample code for Chapter 3 - "Dictionaries and sets"
|
||||
|
||||
From the book "Fluent Python" by Luciano Ramalho (O'Reilly, 2015)
|
||||
http://shop.oreilly.com/product/0636920032519.do
|
@ -3,10 +3,9 @@
|
||||
This module and ``test_transformdict.py`` were extracted from a
|
||||
patch contributed to Python by Antoine Pitrou implementing his
|
||||
PEP 455 -- Adding a key-transforming dictionary to collections.
|
||||
|
||||
As of Nov. 14, 2014, the patch was not yet merged to Python 3.5
|
||||
(which is in pre-alpha). The patch is ``transformdict3.patch``,
|
||||
part of issue #18986: Add a case-insensitive case-preserving dict.
|
||||
That PEP was rejected, and the patch was never merged to CPython.
|
||||
The original code is in ``transformdict3.patch``, part of
|
||||
issue #18986: Add a case-insensitive case-preserving dict.
|
||||
|
||||
http://bugs.python.org/issue18986
|
||||
"""
|
||||
|
21
03-dict-set/zen.txt
Normal file
21
03-dict-set/zen.txt
Normal file
@ -0,0 +1,21 @@
|
||||
The Zen of Python, by Tim Peters
|
||||
|
||||
Beautiful is better than ugly.
|
||||
Explicit is better than implicit.
|
||||
Simple is better than complex.
|
||||
Complex is better than complicated.
|
||||
Flat is better than nested.
|
||||
Sparse is better than dense.
|
||||
Readability counts.
|
||||
Special cases aren't special enough to break the rules.
|
||||
Although practicality beats purity.
|
||||
Errors should never pass silently.
|
||||
Unless explicitly silenced.
|
||||
In the face of ambiguity, refuse the temptation to guess.
|
||||
There should be one-- and preferably only one --obvious way to do it.
|
||||
Although that way may not be obvious at first unless you're Dutch.
|
||||
Now is better than never.
|
||||
Although never is often better than *right* now.
|
||||
If the implementation is hard to explain, it's a bad idea.
|
||||
If the implementation is easy to explain, it may be a good idea.
|
||||
Namespaces are one honking great idea -- let's do more of those!
|
Loading…
Reference in New Issue
Block a user