{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1 align=\"center\">Itertools module</h1>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from itertools import starmap\n",
"\n",
"evaluated_values = starmap(lambda x, y: x + x, [(1, 2), (3, 4), (5, 6), (7, 8)])\n",
"for i in evaluated_values:\n",
" print(i, end = ' ')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from itertools import zip_longest\n",
"\n",
"print(list(zip_longest([1, 2, 3], [11, 12], [21, 22, 23, 25], [31, 32])))\n",
"print(list(zip_longest([1, 2, 3], [11, 12], [21, 22, 23, 25], [31, 32], fillvalue = -1)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from operator import mul\n",
"from itertools import accumulate\n",
"\n",
"print(tuple(accumulate([0, 1, 2, 3, 4, 5])))\n",
"print(tuple(accumulate([1, 2, 3, 4, 5], mul)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from itertools import combinations\n",
"\n",
"print(list(combinations(range(4), 0)))\n",
"print(tuple(combinations(range(4), 2)))\n",
"print(set(combinations(range(4), 4)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from itertools import combinations_with_replacement\n",
"\n",
"print(list(combinations_with_replacement(range(4), 1)))\n",
"print(tuple(combinations_with_replacement(range(4), 2)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from itertools import permutations\n",
"\n",
"print(list(permutations(range(4), 0)))\n",
"print(tuple(permutations(range(4), 2)))\n",
"print(set(permutations(range(4))))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from itertools import product\n",
"\n",
"for i in product(range(3), range(2), range(4)):\n",
" print(i)\n",
"print()\n",
"\n",
"for i in product(range(2), repeat = 4):\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from itertools import count\n",
"\n",
"# Arguments \"start\" and \"step\" take default values of 0 and 1, respectively.\n",
"counter = count()\n",
"for i in counter:\n",
" if i > 9:\n",
" break\n",
" print(i, end = ' ')\n",
"print()\n",
"\n",
"counter = count(10)\n",
"for i in counter:\n",
" if i > 19:\n",
" break\n",
" print(i, end = ' ')\n",
"print()\n",
"\n",
"counter = count(10, 2)\n",
"for i in counter:\n",
" if i > 19:\n",
" break\n",
" print(i, end = ' ')\n",
"print()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from itertools import tee\n",
"\n",
"copies = tee(range(4))\n",
"for i in copies:\n",
" print(list(i))\n",
"print()\n",
"\n",
"copies = tee(range(4), 5)\n",
"for i in copies:\n",
" print(list(i))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from itertools import repeat\n",
"\n",
"to_be_repeated = repeat(range(3))\n",
"for i in range(6):\n",
" print(set(next(to_be_repeated)), end = ' ')\n",
"print()\n",
"\n",
"to_be_repeated = repeat(range(3), 6)\n",
"for i in to_be_repeated:\n",
" print(set(i), end = ' ')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from itertools import cycle\n",
"\n",
"values_to_cycle_over = cycle(range(4))\n",
"for i in range(10):\n",
" print((i, next(values_to_cycle_over)), end = ' ')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from itertools import chain\n",
"\n",
"for i in chain([1, 2, 3], (4, 5, 6), {7, 8}, {9: 'A', 10: 'B'}):\n",
" print(i, end = ' ')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from itertools import compress\n",
"\n",
"print(list(compress(range(0, 400, 10), (x > 32 for x in range(40)))))\n",
"print(list(compress(range(0, 40), [x % y == 0 for y in range(2, 6) for x in range(10)])))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from itertools import dropwhile\n",
"\n",
"for i in dropwhile(lambda x : x % 3, [1, 4, 5, 2, 9, 3, 6, 0, 8, 7]):\n",
" print(i, end = ' ')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from itertools import takewhile\n",
"\n",
"for i in takewhile(lambda x : x % 3, [1, 4, 5, 2, 9, 3, 6, 0, 8, 7]):\n",
" print(i, end = ' ')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from itertools import filterfalse\n",
"\n",
"for i in filterfalse(lambda x : x % 3, [1, 4, 5, 2, 9, 3, 6, 0, 8, 7]):\n",
" print(i, end = ' ')\n",
"print()\n",
" \n",
"for i in filterfalse(None, [1, 4, 5, 2, 9, 3, 6, 0, 8, 7]):\n",
" print(i, end = ' ') "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from itertools import groupby\n",
"\n",
"key_function = lambda x: x % 3\n",
"# Values with the same key have to be consecutive\n",
"# in order to be grouped together by \"groupby\"\n",
"sorted_values = sorted(range(20), key = key_function)\n",
"groups = groupby(sorted_values, key_function)\n",
"for (key, group) in groups:\n",
" print(key, list(group))\n",
"print()\n",
"\n",
"groups = groupby(sorted_values)\n",
"for (key, group) in groups:\n",
" print(key, list(group))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from itertools import islice\n",
"\n",
"selection = islice(range(20), 15)\n",
"for i in selection:\n",
" print(i, end = ' ')\n",
"print()\n",
"\n",
"selection = islice(range(20), 3, 15)\n",
"for i in selection:\n",
" print(i, end = ' ')\n",
"print()\n",
"\n",
"selection = islice(range(20), 3, 15, 2)\n",
"for i in selection:\n",
" print(i, end = ' ')"
]
}
],
"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.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Resource created Wednesday 09 September 2015, 12:12:16 PM.
file: itertools_module.ipynb