{ "metadata": { "name": "", "signature": "sha256:601aa89dfc05490ee4bf673e2693340eb78b91802c822550db1ff2b26e35069e" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "\u0418\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u044b" ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Range:\n", " def __init__(self, start, stop):\n", " self._current = start\n", " self._stop = stop\n", " \n", " def __iter__(self):\n", " return self\n", " \n", " def __next__(self):\n", " if self._current < self._stop:\n", " result = self._current\n", " self._current += 1\n", " return result\n", " else:\n", " raise StopIteration\n", " \n", "for e in Range(0, 3):\n", " print(e)\n", " \n", "i = Range(0, 3)\n", "print(next(i))\n", "print(next(i))\n", "i = iter(i)\n", "print(next(i))\n", "print(next(i))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n", "2\n", "0\n", "1\n", "2\n" ] }, { "ename": "StopIteration", "evalue": "", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 23\u001b[0m \u001b[0mi\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0miter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 25\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36m__next__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mStopIteration\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 16\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0me\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mRange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mStopIteration\u001b[0m: " ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "\u0413\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def range(start, stop):\n", " current = start\n", " while current < stop:\n", " yield current\n", " current += 1\n", "\n", " \n", "g = range(0, 3)\n", "for e in g:\n", " print(e)\n", "\n", "g = range(0, 3)\n", "print(next(g))\n", "print(next(g))\n", "print(next(g))\n", "print(next(g))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n", "2\n", "0\n", "1\n", "2\n" ] }, { "ename": "StopIteration", "evalue": "", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mStopIteration\u001b[0m: " ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "def range(start, stop):\n", " current = start\n", " while current < stop:\n", " reset = yield current\n", " if reset is not None:\n", " current = reset - 1\n", " current += 1\n", "\n", " \n", "g = range(0, 3)\n", "print(next(g))\n", "print(next(g))\n", "print(g.send(0))\n", "print(next(g))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n", "0\n", "1\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "yield from" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def range(start, stop):\n", " current = start\n", " while current < stop:\n", " reset = yield current\n", " if reset is not None:\n", " current = reset - 1\n", " current += 1\n", "\n", "def two_ranges(start, stop):\n", " for x in range(start, stop):\n", " yield x\n", " for x in range(start, stop):\n", " yield x\n", " \n", "g = two_ranges(0, 3)\n", "print(next(g))\n", "print(next(g))\n", "print(next(g))\n", "print(next(g))\n", "print(next(g))\n", "print(next(g))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n", "2\n", "0\n", "1\n", "2\n" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "g = two_ranges(0, 3)\n", "print(next(g))\n", "print(next(g))\n", "print(g.send(-1))\n", "print(next(g))\n", "print(next(g))\n", "print(next(g))\n", "print(next(g))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n", "2\n", "0\n", "1\n", "2\n" ] }, { "ename": "StopIteration", "evalue": "", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mStopIteration\u001b[0m: " ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "def range(start, stop):\n", " current = start\n", " while current < stop:\n", " reset = yield current\n", " if reset is not None:\n", " current = reset - 1\n", " current += 1\n", "\n", "def two_ranges(start, stop):\n", " yield from range(start, stop)\n", " yield from range(start, stop)\n", " \n", "g = two_ranges(0, 3)\n", "print(next(g))\n", "print(next(g))\n", "print(next(g))\n", "print(next(g))\n", "print(next(g))\n", "print(next(g))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n", "2\n", "0\n", "1\n", "2\n" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "g = two_ranges(0, 3)\n", "print(next(g))\n", "print(next(g))\n", "print(g.send(-1))\n", "print(next(g))\n", "print(next(g))\n", "print(next(g))\n", "print(next(g))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n", "-1\n", "0\n", "1\n", "2\n", "0\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "\u0413\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u043d\u044b\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f" ] }, { "cell_type": "code", "collapsed": false, "input": [ "g = (x for x in [1, 2, 3])\n", "print(next(g))\n", "print(next(g))\n", "print(next(g))\n", "print(next(g))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "2\n", "3\n" ] }, { "ename": "StopIteration", "evalue": "", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mStopIteration\u001b[0m: " ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "g = (x * x for x in [1, 2, 3])\n", "for e in g:\n", " print(e)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "4\n", "9\n" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "g = (x * x for x in range(10) if x % 2 == 0)\n", "list(g)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "[0, 4, 16, 36, 64]" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "g = (x * y for x in range(3) for y in range(4))\n", "list(g)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "[0, 0, 0, 0, 0, 1, 2, 3, 0, 2, 4, 6]" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Comprehensions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [x * x for x in [1, 2, 3]]\n", "a" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "[1, 4, 9]" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "s = {x * x for x in [1, 2, 3]}\n", "s" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "{1, 4, 9}" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "d = {x: x * x for x in [1, 2, 3]}\n", "d" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "{1: 1, 2: 4, 3: 9}" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "itertools" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import itertools\n", "\n", "\n", "i = itertools.chain(range(3), range(5), range(2))\n", "for e in i:\n", " print(e)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n", "2\n", "0\n", "1\n", "2\n", "3\n", "4\n", "0\n", "1\n" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "i = itertools.combinations(range(4), 3)\n", "list(i)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "[(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)]" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "i = itertools.permutations(range(4), 3)\n", "list(i)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "[(0, 1, 2),\n", " (0, 1, 3),\n", " (0, 2, 1),\n", " (0, 2, 3),\n", " (0, 3, 1),\n", " (0, 3, 2),\n", " (1, 0, 2),\n", " (1, 0, 3),\n", " (1, 2, 0),\n", " (1, 2, 3),\n", " (1, 3, 0),\n", " (1, 3, 2),\n", " (2, 0, 1),\n", " (2, 0, 3),\n", " (2, 1, 0),\n", " (2, 1, 3),\n", " (2, 3, 0),\n", " (2, 3, 1),\n", " (3, 0, 1),\n", " (3, 0, 2),\n", " (3, 1, 0),\n", " (3, 1, 2),\n", " (3, 2, 0),\n", " (3, 2, 1)]" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "i = itertools.cycle(range(4))\n", "print(next(i))\n", "print(next(i))\n", "print(next(i))\n", "print(next(i))\n", "print(next(i))\n", "print(next(i))\n", "print(next(i))\n", "print(next(i))\n", "print(next(i))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "1\n", "2\n", "3\n", "0\n", "1\n", "2\n", "3\n", "0\n" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "i = itertools.product(\"abcdefgh\", range(1, 9))\n", "list(i)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ "[('a', 1),\n", " ('a', 2),\n", " ('a', 3),\n", " ('a', 4),\n", " ('a', 5),\n", " ('a', 6),\n", " ('a', 7),\n", " ('a', 8),\n", " ('b', 1),\n", " ('b', 2),\n", " ('b', 3),\n", " ('b', 4),\n", " ('b', 5),\n", " ('b', 6),\n", " ('b', 7),\n", " ('b', 8),\n", " ('c', 1),\n", " ('c', 2),\n", " ('c', 3),\n", " ('c', 4),\n", " ('c', 5),\n", " ('c', 6),\n", " ('c', 7),\n", " ('c', 8),\n", " ('d', 1),\n", " ('d', 2),\n", " ('d', 3),\n", " ('d', 4),\n", " ('d', 5),\n", " ('d', 6),\n", " ('d', 7),\n", " ('d', 8),\n", " ('e', 1),\n", " ('e', 2),\n", " ('e', 3),\n", " ('e', 4),\n", " ('e', 5),\n", " ('e', 6),\n", " ('e', 7),\n", " ('e', 8),\n", " ('f', 1),\n", " ('f', 2),\n", " ('f', 3),\n", " ('f', 4),\n", " ('f', 5),\n", " ('f', 6),\n", " ('f', 7),\n", " ('f', 8),\n", " ('g', 1),\n", " ('g', 2),\n", " ('g', 3),\n", " ('g', 4),\n", " ('g', 5),\n", " ('g', 6),\n", " ('g', 7),\n", " ('g', 8),\n", " ('h', 1),\n", " ('h', 2),\n", " ('h', 3),\n", " ('h', 4),\n", " ('h', 5),\n", " ('h', 6),\n", " ('h', 7),\n", " ('h', 8)]" ] } ], "prompt_number": 12 } ], "metadata": {} } ] }