Skip to content

Commit d0feeee

Browse files
committed
Itertools split
1 parent 4dafb0d commit d0feeee

File tree

1 file changed

+64
-77
lines changed

1 file changed

+64
-77
lines changed

README.md

Lines changed: 64 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,46 @@ Generator
206206
**Convenient way to implement the iterator protocol.**
207207

208208
```python
209-
def step(start, step_size):
209+
def count(start, step):
210210
while True:
211211
yield start
212-
start += step_size
212+
start += step
213213
```
214214

215215
```python
216-
>>> stepper = step(10, 2)
217-
>>> next(stepper), next(stepper), next(stepper)
216+
>>> counter = count(10, 2)
217+
>>> next(counter), next(counter), next(counter)
218218
(10, 12, 14)
219219
```
220220

221221

222+
Itertools
223+
---------
224+
* **Every function returns an iterator and can accept any collection and/or iterator.**
225+
* **If you want to print the iterator, you need to pass it to the list() function!**
226+
227+
```python
228+
from itertools import islice, count, repeat, cycle, chain
229+
```
230+
231+
```python
232+
<iter> = islice(<collection>, to_exclusive)
233+
<iter> = islice(<collection>, from_inclusive, to_exclusive)
234+
<iter> = islice(<collection>, from_inclusive, to_exclusive, step_size)
235+
```
236+
237+
```python
238+
<iter> = count(start=0, step=1) # Counter.
239+
<iter> = repeat(<el> [, times]) # Returns element endlesly or times times.
240+
<iter> = cycle(<collection>) # Repeats the sequence indefinately.
241+
```
242+
243+
```python
244+
<iter> = chain(<collection>, <collection> [, ...]) # Empties sequences in order.
245+
<iter> = chain.from_iterable(<collection>) # Empties sequences inside a sequence in order.
246+
```
247+
248+
222249
Type
223250
----
224251
```python
@@ -427,6 +454,39 @@ shuffle(<list>)
427454
```
428455

429456

457+
Combinatorics
458+
-------------
459+
* **Every function returns an iterator.**
460+
* **If you want to print the iterator, you need to pass it to the list() function!**
461+
462+
```python
463+
from itertools import combinations, combinations_with_replacement, permutations, product
464+
```
465+
466+
```python
467+
>>> combinations('abc', 2)
468+
[('a', 'b'), ('a', 'c'), ('b', 'c')]
469+
470+
>>> combinations_with_replacement('abc', 2)
471+
[('a', 'a'), ('a', 'b'), ('a', 'c'),
472+
('b', 'b'), ('b', 'c'),
473+
('c', 'c')]
474+
475+
>>> permutations('abc', 2)
476+
[('a', 'b'), ('a', 'c'),
477+
('b', 'a'), ('b', 'c'),
478+
('c', 'a'), ('c', 'b')]
479+
480+
>>> product('ab', '12')
481+
[('a', '1'), ('a', '2'),
482+
('b', '1'), ('b', '2')]
483+
484+
>>> product([0, 1], repeat=3)
485+
[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
486+
(1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
487+
```
488+
489+
430490
Datetime
431491
--------
432492
```python
@@ -1338,79 +1398,6 @@ Hashlib
13381398
```
13391399

13401400

1341-
Itertools
1342-
---------
1343-
* **Every function returns an iterator and can accept any collection and/or iterator.**
1344-
* **If you want to print the iterator, you need to pass it to the list() function!**
1345-
1346-
```python
1347-
from itertools import *
1348-
```
1349-
1350-
### Combinatoric iterators
1351-
```python
1352-
>>> combinations('abc', 2)
1353-
[('a', 'b'), ('a', 'c'), ('b', 'c')]
1354-
1355-
>>> combinations_with_replacement('abc', 2)
1356-
[('a', 'a'), ('a', 'b'), ('a', 'c'),
1357-
('b', 'b'), ('b', 'c'),
1358-
('c', 'c')]
1359-
1360-
>>> permutations('abc', 2)
1361-
[('a', 'b'), ('a', 'c'),
1362-
('b', 'a'), ('b', 'c'),
1363-
('c', 'a'), ('c', 'b')]
1364-
1365-
>>> product('ab', '12')
1366-
[('a', '1'), ('a', '2'),
1367-
('b', '1'), ('b', '2')]
1368-
1369-
>>> product([0, 1], repeat=3)
1370-
[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
1371-
(1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
1372-
```
1373-
1374-
### Infinite iterators
1375-
```python
1376-
>>> a = count(5, 2)
1377-
>>> next(a), next(a), next(a)
1378-
(5, 7, 9)
1379-
1380-
>>> a = cycle('abc')
1381-
>>> [next(a) for _ in range(10)]
1382-
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a']
1383-
1384-
>>> repeat(10, 3)
1385-
[10, 10, 10]
1386-
```
1387-
1388-
### Iterators
1389-
```python
1390-
>>> chain([1, 2], [3, 4])
1391-
[1, 2, 3, 4]
1392-
1393-
>>> # islice(<collection>, from_inclusive, to_exclusive)
1394-
>>> islice([1, 2, 3, 4], 2, None)
1395-
[3, 4]
1396-
1397-
>>> compress([1, 2, 3, 4], [True, False, 1, 0])
1398-
[1, 3]
1399-
```
1400-
1401-
### Group by
1402-
```python
1403-
>>> people = [{'id': 1, 'name': 'Bob'},
1404-
{'id': 2, 'name': 'Bob'},
1405-
{'id': 3, 'name': 'Peter'}]
1406-
>>> groups = groupby(people, key=lambda a: a['name'])
1407-
>>> {name: list(group) for name, group in groups}
1408-
{'Bob': [{'id': 1, 'name': 'Bob'},
1409-
{'id': 2, 'name': 'Bob'}],
1410-
'Peter': [{'id': 3, 'name': 'Peter'}]}
1411-
```
1412-
1413-
14141401
Introspection and Metaprograming
14151402
--------------------------------
14161403
**Inspecting code at runtime and code that generates code. You can:**

0 commit comments

Comments
 (0)