Last active
March 29, 2020 08:56
-
-
Save codebrain001/8537e6fb2887d20556d28c5bdab7584a to your computer and use it in GitHub Desktop.
A tactile guide to Python Collections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The Collections module is a built-in module and comes with a base installation of Python 3, and therefore doesn't need to be installed (and will not be found on PyPI)." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### 1. **namedtuple**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from collections import namedtuple\n", | |
"# The first argument of namedtuple constructor, is the typename: the name of the named tuple, not the arguments." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Let's prepare a namedtuple for individuals and main of the building they reside\n", | |
"buildings = namedtuple('buildings','Lulu,Tanzanite, Masetha,Dhahabu')\n", | |
"# building is an object of the namedtuple class, which has been initialized here\n", | |
"names = buildings(('Leki', 'Abel', 'Wuraola'), 'Patrick', 'Yemi', 'Brain')\n", | |
"# here, we are defining the field name with the instance of the building class " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"('Leki', 'Abel', 'Wuraola')\n" | |
] | |
} | |
], | |
"source": [ | |
"print(names.Lulu)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Patrick\n" | |
] | |
} | |
], | |
"source": [ | |
"print(names.Tanzanite)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We can create a namedtuple from a list, it can be carried out as follows:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"buildings(Lulu='Sandra', Tanzanite='Rita', Masetha='Remi', Dhahabu='Peter')\n" | |
] | |
} | |
], | |
"source": [ | |
"names2 = buildings._make(['Sandra', 'Rita', 'Remi', 'Peter'])\n", | |
"print(names2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Sandra\n" | |
] | |
} | |
], | |
"source": [ | |
"print(names2.Lulu)" | |
] | |
}, | |
{ | |
"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.7.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment