{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Comprehensions are good, but not memory efficient. They may create\ta\twhole\tnew\tlist" ] }, { "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": "markdown", "metadata": {}, "source": [ "### Need to \thold the\tlength\tof\tevery\tline\tof\tthe file\tin\tmemory. BAD!" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[94, 48, 19, 7, 27, 36, 92, 33, 25, 9]\n" ] } ], "source": [ "# Example 1\n", "import random\n", "with open('my_file.txt', 'w') as f:\n", " for _ in range(10):\n", " f.write('a' * random.randint(0, 100))\n", " f.write('\\n')\n", "\n", "value = [len(x) for x in open('my_file.txt')]\n", "print(value)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### A\tgenerator\texpression () does not make forward progress. GOOD!" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " at 0x103e45e08>\n" ] } ], "source": [ "# Example 2\n", "it = (len(x) for x in open('my_file.txt'))\n", "print(it)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "### It returns iterator." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "94\n", "48\n" ] } ], "source": [ "# Example 3\n", "print(next(it))\n", "print(next(it))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### generator\texpressions\tcan\tbe\tcomposed\ttogether. Create a new generator using 'it'! WONDERFUL!!" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " at 0x103e45eb8>\n" ] } ], "source": [ "# Example 4\n", "roots = ((x, x**0.5) for x in it)\n", "print(roots)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(19, 4.358898943540674)\n" ] } ], "source": [ "# Example 5\n", "print(next(roots))" ] } ], "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 }