{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## CS 200: Iterators and Generators\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"Reading: \n",
"- Learning Python, Chapter 14\n",
"- Python Cookbook, Chapter 4\n",
"\n",
"The concept of “iterable objects” is relatively recent in Python,\n",
"but it has come to permeate the language’s design. It’s essentially a\n",
"generalization of the notion of sequences—an object is considered\n",
"iterable if it is either a physically stored sequence, or an object\n",
"that produces one result at a time in the context of an iteration tool\n",
"like a for loop. In a sense, iterable objects include both physical\n",
"sequences and virtual sequences computed on demand.\n",
"\n",
"Below we iterate over a file implicitly using a for loop:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def it0(file=\"iterators.py\"):\n",
" count = 0\n",
" for line in open(file):\n",
" count += 1\n",
" if count < 10:\n",
" print (line, end='')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"#! /usr/bin/python3\n",
"\n",
"'''\n",
"From Learning Python, Chapter 14\n",
"\n",
"The concept of “iterable objects” is relatively recent in Python,\n",
"but it has come to permeate the language’s design. It’s essentially a\n",
"generalization of the notion of sequences—an object is considered\n",
"iterable if it is either a physically stored sequence, or an object\n"
]
}
],
"source": [
"it0()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we iterate explicitly using next
and readline
. Note that print()
adds a newline."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def it1(file=\"iterators.py\"):\n",
" f = open(file)\n",
" print(f.readline())\n",
" print(f.readline())\n",
" print(f.__next__())\n",
" print(f.__next__())\n",
" print(next(f))\n",
" print(next(f))\n",
" f.close()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"#! /usr/bin/python3\n",
"\n",
"\n",
"\n",
"'''\n",
"\n",
"From Learning Python, Chapter 14\n",
"\n",
"\n",
"\n",
"The concept of “iterable objects” is relatively recent in Python,\n",
"\n"
]
}
],
"source": [
"it1()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" \\_\\_next\\_\\_()
and readline()
are equivalent for files.\n",
" They each iterate over the file a line at a time.\n",
"\n",
"\\_\\_next\\_\\_()
is an ITERATOR in Python.\n",
"The for loop calls \\_\\_next\\_\\_()
\n",
"\n",
"next(f)
calls the iterator for f\n",
"\n",
"Can explicitly get (or make) an iterator using iter()
"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def it2(lst = [1,2,3,4]):\n",
" it = iter(lst)\n",
" print (it.__next__())\n",
" print (next(it))\n",
" print (next(it))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n"
]
}
],
"source": [
"it2()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n"
]
}
],
"source": [
"it2([1,2,3,4,5])"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n"
]
},
{
"ename": "StopIteration",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)",
"Input \u001b[0;32mIn [8]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m it2([\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m])\n",
"Input \u001b[0;32mIn [5]\u001b[0m, in \u001b[0;36mit2\u001b[0;34m(lst)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m (it\u001b[38;5;241m.\u001b[39m\u001b[38;5;21m__next__\u001b[39m())\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m (\u001b[38;5;28mnext\u001b[39m(it))\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28mprint\u001b[39m (\u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mit\u001b[49m\u001b[43m)\u001b[49m)\n",
"\u001b[0;31mStopIteration\u001b[0m: "
]
}
],
"source": [
"it2([1,2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Below we manually implement iteration."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"def it4(lst = [1,2,3,4]):\n",
" it = iter(lst)\n",
" while True:\n",
" try:\n",
" x = next(it)\n",
" except StopIteration:\n",
" break\n",
" print (x, ' ', end='')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2 3 4 "
]
}
],
"source": [
"it4()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2 3 4 5 6 7 8 "
]
}
],
"source": [
"it4([1,2,3,4,5,6,7,8])"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"it4([])"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a b c d e f "
]
}
],
"source": [
"it4('abcdef')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Other built-in iterators include dictionaries."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"def it5(dict = {'a':1, 'b':2, 'c': 3}):\n",
" for key in dict.keys():\n",
" print (key, dict[key])"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a 1\n",
"b 2\n",
"c 3\n"
]
}
],
"source": [
"it5()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"it5({})"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"john 20\n",
"mary 30\n",
"joe 45\n",
"anne 23\n"
]
}
],
"source": [
"it5({'john':20, 'mary':30, 'joe':45, 'anne':23})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Range objects are implicit lists.\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"def it6(n = 6):\n",
" R = range(n)\n",
" it = iter(R)\n",
" print (next(it))\n",
" print (next(it))\n",
" print (list(it))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"[2, 3, 4, 5]\n"
]
}
],
"source": [
"it6()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n"
]
},
{
"ename": "StopIteration",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)",
"Input \u001b[0;32mIn [20]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m it6(\u001b[38;5;241m1\u001b[39m)\n",
"Input \u001b[0;32mIn [18]\u001b[0m, in \u001b[0;36mit6\u001b[0;34m(n)\u001b[0m\n\u001b[1;32m 3\u001b[0m it \u001b[38;5;241m=\u001b[39m \u001b[38;5;28miter\u001b[39m(R)\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m (\u001b[38;5;28mnext\u001b[39m(it))\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28mprint\u001b[39m (\u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mit\u001b[49m\u001b[43m)\u001b[49m)\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28mprint\u001b[39m (\u001b[38;5;28mlist\u001b[39m(it))\n",
"\u001b[0;31mStopIteration\u001b[0m: "
]
}
],
"source": [
"it6(1)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"[]\n"
]
}
],
"source": [
"it6(2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Enumerate objects are iterable. \n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"def it7(text = \"hello, world!\"):\n",
" it = iter(enumerate(text))\n",
" print (next(it))\n",
" print (next(it))\n",
" print (list(it))"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(0, 'h')\n",
"(1, 'e')\n",
"[(2, 'l'), (3, 'l'), (4, 'o'), (5, ','), (6, ' '), (7, 'w'), (8, 'o'), (9, 'r'), (10, 'l'), (11, 'd'), (12, '!')]\n"
]
}
],
"source": [
"it7()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can iterate through the output of unix commands."
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"def it8(cmd = 'ls -l'):\n",
" p = os.popen(cmd)\n",
" count = 0\n",
" for x in p:\n",
" count += 1\n",
" if count < 10:\n",
" print (x, end='')"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"total 31924\n",
"-rw-r--r-- 1 sbs5 cs200ta 5619 Aug 31 12:46 0831.html\n",
"-rw-rw-r-- 1 sbs5 sbs5 620327 Aug 1 18:57 0831nb.html\n",
"-rw-rw-r-- 1 sbs5 cs200ta 33828 Aug 1 18:57 0831nb.ipynb\n",
"-rw-rw-r-- 1 sbs5 cs200ta 1834 Aug 31 15:32 0831.script\n",
"-rw-r--r-- 1 sbs5 cs200ta 4919 Oct 7 08:40 0902.html\n",
"-rw-rw-r-- 1 sbs5 cs200ta 24567 Sep 2 15:38 0902.script\n",
"-rw-r--r-- 1 sbs5 cs200ta 4567 Oct 7 08:40 0907.html\n",
"-rw-rw-r-- 1 sbs5 cs200ta 27126 Sep 7 16:08 0907.script\n"
]
}
],
"source": [
"it8()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"map is an iterable."
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"def it9(lst = [1,2,3,4,5,6,7,8]):\n",
" x = map(lambda x: x*x, lst)\n",
" print (x)\n",
" for e in x:\n",
" print (e)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" | |