{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Advanced Dictionary Topics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "built-in ** zip() ** function\n", "- all builtin functions are listed here with examples: https://docs.python.org/3/library/functions.html" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "help(zip)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "zdata = zip([1, 2, 3], ('a', 'b', 'c'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(zdata)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "alist = list(zdata)\n", "print(alist)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "adict = dict(alist)\n", "print(adict)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## exercise\n", "Create a dict that maps lowercase alphabets to integers, e.g., a maps to 1, b maps to 2, ..., z maps to 26 and print it" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import string\n", "lettersToDigits = dict(zip(string.ascii_lowercase, range(1, 27)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(lettersToDigits)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ordered dictionary\n", "- defined in collections module" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from collections import OrderedDict\n", "bdict = OrderedDict()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "bdict['z'] = 26\n", "bdict['a'] = 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(bdict)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## exercise\n", "Create a dict that maps lowercase alphabets to their corresponding ASCII values , e.g., a maps to 97, b maps to 98, ..., z maps to 122 and print the dictionary in alphabetical order" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import string\n", "lettersToDigits = dict(zip(string.ascii_lowercase, range(ord('a'), ord('z')+1)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(lettersToDigits)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "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.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }