{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n", "#\n", "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# http://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License.\n", "\n", "# Preamble to mimick book environment\n", "import logging\n", "from pprint import pprint\n", "from sys import stdout as STDOUT" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "# Example 1\n", "from collections import deque\n", "fifo = deque()\n", "fifo.append(1) # Producer\n", "fifo.append(2)\n", "fifo.append(3)\n", "x = fifo.popleft() # Consumer\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'bar': 2, 'foo': 1}\n", "{'foo': 1, 'bar': 2}\n", "Equal? True\n" ] } ], "source": [ "# Example 2\n", "a = {}\n", "a['foo'] = 1\n", "a['bar'] = 2\n", "from random import randint\n", "\n", "# Randomly populate 'b' to cause hash conflicts\n", "while True:\n", " z = randint(99, 1013)\n", " b = {}\n", " for i in range(z):\n", " b[i] = i\n", " b['foo'] = 1\n", " b['bar'] = 2\n", " for i in range(z):\n", " del b[i]\n", " if str(b) != str(a):\n", " break\n", "\n", "print(a)\n", "print(b)\n", "print('Equal?', a == b)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 red\n", "2 blue\n" ] } ], "source": [ "# Example 3\n", "from collections import OrderedDict\n", "a = OrderedDict()\n", "a['foo'] = 1\n", "a['bar'] = 2\n", "\n", "b = OrderedDict()\n", "b['foo'] = 'red'\n", "b['bar'] = 'blue'\n", "\n", "for value1, value2 in zip(a.values(), b.values()):\n", " print(value1, value2)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'my_counter': 1}\n" ] } ], "source": [ "# Example 4\n", "stats = {}\n", "key = 'my_counter'\n", "if key not in stats:\n", " stats[key] = 0\n", "stats[key] += 1\n", "print(stats)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'my_counter': 1}\n" ] } ], "source": [ "# Example 5\n", "from collections import defaultdict\n", "stats = defaultdict(int)\n", "stats['my_counter'] += 1\n", "print(dict(stats))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 6\n", "from heapq import *\n", "a = []\n", "heappush(a, 5)\n", "heappush(a, 3)\n", "heappush(a, 7)\n", "heappush(a, 4)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3 4 5 7\n" ] } ], "source": [ "# Example 7\n", "print(heappop(a), heappop(a), heappop(a), heappop(a))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 8\n", "a = []\n", "heappush(a, 5)\n", "heappush(a, 3)\n", "heappush(a, 7)\n", "heappush(a, 4)\n", "assert a[0] == nsmallest(1, a)[0] == 3" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Before: [3, 4, 7, 5]\n", "After: [3, 4, 5, 7]\n" ] } ], "source": [ "# Example 9\n", "print('Before:', a)\n", "a.sort()\n", "print('After: ', a)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "991234\n" ] } ], "source": [ "# Example 10\n", "x = list(range(10**6))\n", "i = x.index(991234)\n", "print(i)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "991234\n" ] } ], "source": [ "# Example 11\n", "from bisect import bisect_left\n", "i = bisect_left(x, 991234)\n", "print(i)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.0029635409882757813\n", "0.0016338950081262738\n" ] } ], "source": [ "# Example 12\n", "from timeit import timeit\n", "print(timeit(\n", " 'a.index(len(a)-1)',\n", " 'a = list(range(100))',\n", " number=1000))\n", "print(timeit(\n", " 'bisect_left(a, len(a)-1)',\n", " 'from bisect import bisect_left;'\n", " 'a = list(range(10**6))',\n", " number=1000))" ] } ], "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.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }