{ "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": "markdown", "metadata": {}, "source": [ "### support\tmultiple\tlevels\tof\tlooping!" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n" ] } ], "source": [ "# Example 1\n", "matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n", "flat = [x for row in matrix for x in row]\n", "print(flat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### two-dimensional\tmatrix, but still readable." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1, 4, 9], [16, 25, 36], [49, 64, 81]]\n" ] } ], "source": [ "# Example 2\n", "squared = [[x**2 for x in row] for row in matrix]\n", "print(squared)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### another\tloop? Getting complicated!!" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]\n" ] } ], "source": [ "# Example 3\n", "my_lists = [\n", " [[1, 2, 3], [4, 5, 6]],\n", " [[7, 8, 9], [10, 11, 12]],\n", "]\n", "flat = [x for sublist1 in my_lists\n", " for sublist2 in sublist1\n", " for x in sublist2]\n", "print(flat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Perhaps, using loops is better." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]\n" ] } ], "source": [ "# Example 4\n", "flat = []\n", "for sublist1 in my_lists:\n", " for sublist2 in sublist1:\n", " flat.extend(sublist2)\n", "print(flat)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[6, 8, 10]\n", "[6, 8, 10]\n" ] } ], "source": [ "# Example 5\n", "a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "b = [x for x in a if x > 4 if x % 2 == 0]\n", "c = [x for x in a if x > 4 and x % 2 == 0]\n", "print(b)\n", "print(c)\n", "assert b and c\n", "assert b == c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### It's short, but hard to read. Void if possible. " ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[6], [9]]\n" ] } ], "source": [ "# Example 6\n", "matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n", "filtered = [[x for x in row if x % 3 == 0]\n", " for row in matrix if sum(row) >= 10]\n", "print(filtered)" ] } ], "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 }