Browse Source

Itertools

pull/3/head
Jure Šorn 6 years ago
parent
commit
5282a8edd8
1 changed files with 33 additions and 14 deletions
  1. 47
      README.md

47
README.md

@ -750,51 +750,61 @@ Itertools
from itertools import * from itertools import *
``` ```
### Chain
```python
>>> chain([1, 2], range(3, 5))
[1, 2, 3, 4]
```
### Math
### Combinations
#### Combinations:
```python ```python
>>> combinations('abc', 2) >>> combinations('abc', 2)
[('a', 'b'), ('a', 'c'), ('b', 'c')] [('a', 'b'), ('a', 'c'), ('b', 'c')]
``` ```
### Permutations
#### Combinations with Replacement:
```python
>>> ombinations_with_replacement('abc', 2)
[('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'b'), ('b', 'c'), ('c', 'c')]
```
#### Permutations:
```python ```python
>>> permutations('abc', 2) >>> permutations('abc', 2)
[('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')] [('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')]
``` ```
### Product
#### Product:
```python ```python
>>> list(product('ab', [1, 2]))
>>> product('ab', [1, 2])
[('a', 1), ('a', 2), ('b', 1), ('b', 2)] [('a', 1), ('a', 2), ('b', 1), ('b', 2)]
``` ```
### Compress
### Util
#### Chain:
```python
>>> chain([1, 2], range(3, 5))
[1, 2, 3, 4]
```
#### Compress:
```python ```python
>>> compress('abc', [True, 0, 23]) >>> compress('abc', [True, 0, 23])
['a', 'c'] ['a', 'c']
``` ```
### Count
### Count:
```python ```python
>>> i = count(5, 2) >>> i = count(5, 2)
>>> next(i), next(i) >>> next(i), next(i)
(5, 7) (5, 7)
``` ```
### Cycle
#### Cycle:
```python ```python
>>> a = cycle('abc') >>> a = cycle('abc')
>>> [next(a) for _ in range(10)] >>> [next(a) for _ in range(10)]
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a'] ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a']
``` ```
### Groupby
#### Groupby:
```python ```python
>>> a = [{'id': 1, 'name': 'bob'}, >>> a = [{'id': 1, 'name': 'bob'},
{'id': 2, 'name': 'bob'}, {'id': 2, 'name': 'bob'},
@ -805,7 +815,7 @@ from itertools import *
'peter': [{'id': 3, 'name': 'peter'}]} 'peter': [{'id': 3, 'name': 'peter'}]}
``` ```
### Islice
#### Islice:
```python ```python
islice([1, 2, 3], 1, None) islice([1, 2, 3], 1, None)
[2, 3] [2, 3]
@ -850,6 +860,14 @@ False
>>> setattr(z, 'c', 10) >>> setattr(z, 'c', 10)
``` ```
### Parameters
```python
>>> from inspect import signature
>>> sig = signature(bla)
>>> len(sig.parameters)
3
```
### Type ### Type
**Type is the root class. If only passed the object it returns it's type. Otherwise it creates a new class (and not the instance!):** **Type is the root class. If only passed the object it returns it's type. Otherwise it creates a new class (and not the instance!):**
```python ```python
@ -861,6 +879,7 @@ type(class_name, parents<tuple>, attributes<dict>)
>>> z = Z() >>> z = Z()
``` ```
### MetaClass ### MetaClass
#### Class that creates class: #### Class that creates class:
```python ```python

Loading…
Cancel
Save