From d0feeee432ab307304de1bc5b928948b314dbcc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Wed, 27 Feb 2019 13:04:03 +0100 Subject: [PATCH] Itertools split --- README.md | 141 +++++++++++++++++++++++++----------------------------- 1 file changed, 64 insertions(+), 77 deletions(-) diff --git a/README.md b/README.md index 3421d12..0dcb932 100644 --- a/README.md +++ b/README.md @@ -206,19 +206,46 @@ Generator **Convenient way to implement the iterator protocol.** ```python -def step(start, step_size): +def count(start, step): while True: yield start - start += step_size + start += step ``` ```python ->>> stepper = step(10, 2) ->>> next(stepper), next(stepper), next(stepper) +>>> counter = count(10, 2) +>>> next(counter), next(counter), next(counter) (10, 12, 14) ``` +Itertools +--------- +* **Every function returns an iterator and can accept any collection and/or iterator.** +* **If you want to print the iterator, you need to pass it to the list() function!** + +```python +from itertools import islice, count, repeat, cycle, chain +``` + +```python + = islice(, to_exclusive) + = islice(, from_inclusive, to_exclusive) + = islice(, from_inclusive, to_exclusive, step_size) +``` + +```python + = count(start=0, step=1) # Counter. + = repeat( [, times]) # Returns element endlesly or times times. + = cycle() # Repeats the sequence indefinately. +``` + +```python + = chain(, [, ...]) # Empties sequences in order. + = chain.from_iterable() # Empties sequences inside a sequence in order. +``` + + Type ---- ```python @@ -427,6 +454,39 @@ shuffle() ``` +Combinatorics +------------- +* **Every function returns an iterator.** +* **If you want to print the iterator, you need to pass it to the list() function!** + +```python +from itertools import combinations, combinations_with_replacement, permutations, product +``` + +```python +>>> combinations('abc', 2) +[('a', 'b'), ('a', 'c'), ('b', 'c')] + +>>> combinations_with_replacement('abc', 2) +[('a', 'a'), ('a', 'b'), ('a', 'c'), + ('b', 'b'), ('b', 'c'), + ('c', 'c')] + +>>> permutations('abc', 2) +[('a', 'b'), ('a', 'c'), + ('b', 'a'), ('b', 'c'), + ('c', 'a'), ('c', 'b')] + +>>> product('ab', '12') +[('a', '1'), ('a', '2'), + ('b', '1'), ('b', '2')] + +>>> product([0, 1], repeat=3) +[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), + (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)] +``` + + Datetime -------- ```python @@ -1338,79 +1398,6 @@ Hashlib ``` -Itertools ---------- -* **Every function returns an iterator and can accept any collection and/or iterator.** -* **If you want to print the iterator, you need to pass it to the list() function!** - -```python -from itertools import * -``` - -### Combinatoric iterators -```python ->>> combinations('abc', 2) -[('a', 'b'), ('a', 'c'), ('b', 'c')] - ->>> combinations_with_replacement('abc', 2) -[('a', 'a'), ('a', 'b'), ('a', 'c'), - ('b', 'b'), ('b', 'c'), - ('c', 'c')] - ->>> permutations('abc', 2) -[('a', 'b'), ('a', 'c'), - ('b', 'a'), ('b', 'c'), - ('c', 'a'), ('c', 'b')] - ->>> product('ab', '12') -[('a', '1'), ('a', '2'), - ('b', '1'), ('b', '2')] - ->>> product([0, 1], repeat=3) -[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), - (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)] -``` - -### Infinite iterators -```python ->>> a = count(5, 2) ->>> next(a), next(a), next(a) -(5, 7, 9) - ->>> a = cycle('abc') ->>> [next(a) for _ in range(10)] -['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a'] - ->>> repeat(10, 3) -[10, 10, 10] -``` - -### Iterators -```python ->>> chain([1, 2], [3, 4]) -[1, 2, 3, 4] - ->>> # islice(, from_inclusive, to_exclusive) ->>> islice([1, 2, 3, 4], 2, None) -[3, 4] - ->>> compress([1, 2, 3, 4], [True, False, 1, 0]) -[1, 3] -``` - -### Group by -```python ->>> people = [{'id': 1, 'name': 'Bob'}, - {'id': 2, 'name': 'Bob'}, - {'id': 3, 'name': 'Peter'}] ->>> groups = groupby(people, key=lambda a: a['name']) ->>> {name: list(group) for name, group in groups} -{'Bob': [{'id': 1, 'name': 'Bob'}, - {'id': 2, 'name': 'Bob'}], - 'Peter': [{'id': 3, 'name': 'Peter'}]} -``` - - Introspection and Metaprograming -------------------------------- **Inspecting code at runtime and code that generates code. You can:**