Skip to content

Commit 91fb9cf

Browse files
committed
[add] combinatoric iterators
1 parent 41c365c commit 91fb9cf

File tree

4 files changed

+211
-25
lines changed

4 files changed

+211
-25
lines changed

README.md

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ All skills are base on the implementation of Python 3.
3535
<li><a href="#self-class-instance">self (class instance)</li>
3636
<li><a href="#variables-class--instance">variables (class & instance)</li>
3737
<li><a href="#method-vs-classmethod-vs-staticmethod">method vs. classmethod vs. staticmethod</li>
38-
<li><a href="#\_-private-vs-\__-name-mangling">_ (private) vs. __ (name mangling)</li>
38+
<li><a href="#private-vs-name-mangling">_ (private) vs. __ (name mangling)</li>
3939
<li><a href="#property-getter-setter">@property (getter, setter)</li>
4040
<li><a href="#legb-local-enclosing-global-builtins">LEGB (local, enclosing, global, builtins)</li>
4141
<li><a href="#abstract-class">Abstract class</li>
@@ -93,11 +93,14 @@ All skills are base on the implementation of Python 3.
9393
<li><a href="#product">product</li>
9494
<li><a href="#permutations">permutations</li>
9595
<li><a href="#combinations">combinations</li>
96+
<li><a href="#combinations_with_replacement">combinations_with_replacement</li>
9697
</ul>
9798
</td></tr>
9899
</tr>
99100
</table>
100101

102+
103+
101104
# Must Know
102105

103106
## [List & Dict & Set Comprehensions](must_know/list_dict_set_comprehensions.md)
@@ -793,23 +796,78 @@ list(gen) # [('A', 1), ('B', 2), ('C', -1)]
793796

794797
### product
795798

796-
797799
```py
800+
from itertools import product
801+
802+
gen = product("AB", "CD")
803+
list(gen) # [AC, AD, BC, BD]
804+
805+
806+
gen = product("AB", repeat=2)
807+
list(gen) # [AA, AB, BA, BB]
798808

809+
810+
gen = product("AB", "CD", repeat=2)
811+
list(gen)
812+
# [ACAC, ACAD, ACBC, ACBD,
813+
# ADAC, ADAD, ADBC, ADBD,
814+
# BCAC, BCAD, BCBC, BCBD,
815+
# BDAC, BDAD, BDBC, BDBD]
799816
```
800817

801818
### permutations
802819

803-
804820
```py
821+
gen = permutations("ABC") # same as r=3
822+
list(gen) # [ABC, ACB, BAC, BCA, CAB, CBA]
823+
805824

825+
gen = permutations("ABC", r=2)
826+
list(gen) # [AB, AC, BA, BC, CA, CB]
827+
828+
829+
gen = permutations("ABC", r=1)
830+
list(gen) # [A, B, C]
806831
```
807832

808833
### combinations
809834

810-
811835
```py
836+
gen = combinations("ABC", 1)
837+
list(gen)
838+
# [A, B, C]
839+
840+
841+
gen = combinations("ABC", 2)
842+
list(gen)
843+
# [AB, AC, BC]
844+
845+
846+
gen = combinations("ABC", 3)
847+
list(gen)
848+
# [ABC]
849+
```
850+
851+
### combinations_with_replacement
852+
853+
``` py
854+
gen = combinations_with_replacement("ABC", 1)
855+
list(gen)
856+
# [A, B, C]
857+
858+
859+
gen = combinations_with_replacement("ABC", 2)
860+
list(gen)
861+
# [AA, AB, AC,
862+
# BB, BC,
863+
# CC]
864+
812865

866+
gen = combinations_with_replacement("ABC", 3)
867+
list(gen)
868+
# [AAA, AAB, AAC, ABB, ABC, ACC,
869+
# BBB, BBC, BCC,
870+
# CCC]
813871
```
814872

815873
## os

itertools/combinatoric_iterators.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Combinatoric Iterators
2+
3+
The `itertools` is a built-in module in Python 3 and is helpful for us to use a more pythonic way to manipulate the iterables (e.g., list, set, tuple, etc.).
4+
5+
Another advantage of `itertools` is it also returns the [generator](../must_know/generator.md) type instances that have benefits of **lazy evaluation** from all of its methods.
6+
7+
In this section, we will discuss the third part of `itertools`. The methods in this section are all about the combinatoric methods in discrete mathematics.
8+
9+
## Table of Contents
10+
11+
* [Combinatoric Iterators](#combinatoric-iterators)
12+
* [Table of Contents](#table-of-contents)
13+
* [product](#product)
14+
* [permutations](#permutations)
15+
* [combinations](#combinations)
16+
* [combinations_with_replacement](#combinations_with_replacement)
17+
* [Related Articles](#related-articles)
18+
19+
## product
20+
21+
This table takes [Python documentation #product](https://docs.python.org/3/library/itertools.html#itertools.product) as reference.
22+
23+
| Arguments | Results | Example |
24+
| ----------------------- | -------------------------------- | -------------------------------------------------------------- |
25+
| `iterables`, `repeat=1` | cartesian products (nested-loop) | `product("ABC", repeat=2)= AA, AB, AC, BA, BB, BC, CA, CB, CC` |
26+
27+
28+
``` py
29+
from itertools import product
30+
31+
gen = product("AB", "CD")
32+
list(gen) # [AC, AD, BC, BD]
33+
34+
35+
gen = product("AB", repeat=2)
36+
list(gen) # [AA, AB, BA, BB]
37+
38+
39+
gen = product("AB", "CD", repeat=2)
40+
list(gen)
41+
# [ACAC, ACAD, ACBC, ACBD,
42+
# ADAC, ADAD, ADBC, ADBD,
43+
# BCAC, BCAD, BCBC, BCBD,
44+
# BDAC, BDAD, BDBC, BDBD]
45+
```
46+
47+
## permutations
48+
49+
This table takes [Python documentation #permutations](https://docs.python.org/3/library/itertools.html#itertools.permutations) as reference.
50+
51+
| Arguments | Results | Example |
52+
| ----------------------------- | --------------------------- | -------------------------------------------------- |
53+
| `iterable`, `r=len(iterable)` | permutation with length `r` | `permutations("ABC", r=2)= AB, AC, BA, BC, CA, CB` |
54+
55+
``` py
56+
gen = permutations("ABC") # same as r=3
57+
list(gen) # [ABC, ACB, BAC, BCA, CAB, CBA]
58+
59+
60+
gen = permutations("ABC", r=2)
61+
list(gen) # [AB, AC, BA, BC, CA, CB]
62+
63+
64+
gen = permutations("ABC", r=1)
65+
list(gen) # [A, B, C]
66+
```
67+
68+
## combinations
69+
70+
This table takes [Python documentation #combinations](https://docs.python.org/3/library/itertools.html#itertools.combinations) as reference.
71+
72+
| Arguments | Results | Example |
73+
| --------------- | --------------------------- | ------------------------------------ |
74+
| `iterable`, `r` | combination with length `r` | `combinations("ABC", 2)= AB, AC, BC` |
75+
76+
``` py
77+
gen = combinations("ABC", 1)
78+
list(gen)
79+
# [A, B, C]
80+
81+
82+
gen = combinations("ABC", 2)
83+
list(gen)
84+
# [AB, AC, BC]
85+
86+
87+
gen = combinations("ABC", 3)
88+
list(gen)
89+
# [ABC]
90+
```
91+
92+
## combinations_with_replacement
93+
94+
This table takes [Python documentation #combinations_with_replacement](https://docs.python.org/3/library/itertools.html#itertools.combinations_with_replacement) as reference.
95+
96+
| Arguments | Results | Example |
97+
| --------------- | ------------------------------------------------- | ----------------------------------------------------------------- |
98+
| `iterable`, `r` | combination with length `r` and repeated elements | `combinations_with_replacement("ABC", 2)= AA, AB, AC, BB, BC, CC` |
99+
100+
``` py
101+
gen = combinations_with_replacement("ABC", 1)
102+
list(gen)
103+
# [A, B, C]
104+
105+
106+
gen = combinations_with_replacement("ABC", 2)
107+
list(gen)
108+
# [AA, AB, AC,
109+
# BB, BC,
110+
# CC]
111+
112+
113+
gen = combinations_with_replacement("ABC", 3)
114+
list(gen)
115+
# [AAA, AAB, AAC, ABB, ABC, ACC,
116+
# BBB, BBC, BCC,
117+
# CCC]
118+
```
119+
120+
121+
# Related Articles
122+
123+
| Article | Link |
124+
| ---------------------------------------------------------------------------- | ---------------------------------------------------------------- |
125+
| Python documentation - itertools | https://docs.python.org/3/library/itertools.html |
126+
| Python Tutorial: Itertools Module - Iterator Functions for Efficient Looping | https://www.youtube.com/watch?v=Qu3dThVy6KQ |
127+
| Python 好用模組介紹 - itertools & more-itertools | https://myapollo.com.tw/zh-tw/python-itertools-more-itertools/ |
128+
| Python標準庫之itertools庫的使用方法 | https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/364249/ |

itertools/infinite_iterators.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The `itertools` is a built-in module in Python 3 and is helpful for us to use a more pythonic way to manipulate the iterables (e.g., list, set, tuple, etc.).
44

5-
Another advantage of `itertools` is it also returns the [generator](../must_know/generator.md) type instances that have benefits of **lazy evaluation** from all its methods.
5+
Another advantage of `itertools` is it also returns the [generator](../must_know/generator.md) type instances that have benefits of **lazy evaluation** from all of its methods.
66

77
In this section, we will discuss the first part of `itertools`. The methods in this part all return a generator that generates an infinite number of items and won't stop.
88

@@ -19,9 +19,9 @@ In this section, we will discuss the first part of `itertools`. The methods in t
1919

2020
This table takes [Python documentation #count](https://docs.python.org/3/library/itertools.html#itertools.count) as reference..
2121

22-
| Arguments | Results | Example |
23-
| --------------- | ----------------------------------- | -------------------------------------- |
24-
| start, [step=1] | start, start+step, start+2step, ... | `count(2.5, 0.5) = 2.5 3.0 3.5 4.0...` |
22+
| Arguments | Results | Example |
23+
| ----------------- | ----------------------------------- | -------------------------------------- |
24+
| `start, [step=1]` | start, start+step, start+2step, ... | `count(2.5, 0.5) = 2.5 3.0 3.5 4.0...` |
2525

2626

2727
``` py
@@ -40,7 +40,7 @@ This table takes [Python documentation #cycle](https://docs.python.org/3/library
4040

4141
| Arguments | Results | Example |
4242
| -------------- | ------------------------------------ | ---------------------------------- |
43-
| p (`iterable`) | p[0], p[1], ... p[-1], p[0], p[1]... | `cycle([1, 2, 3]) = 1 2 3 1 2 ...` |
43+
| `p (iterable)` | p[0], p[1], ... p[-1], p[0], p[1]... | `cycle([1, 2, 3]) = 1 2 3 1 2 ...` |
4444

4545
``` py
4646
from itertools import cycle
@@ -56,9 +56,9 @@ for x in gen:
5656

5757
This table takes [Python documentation #repeat](https://docs.python.org/3/library/itertools.html#itertools.repeat) as reference.
5858

59-
| Arguments | Results | Example |
60-
| -------------- | ---------------------------------------------- | --------------------------------- |
61-
| item, [n=None] | item, item, item, … endlessly or up to n times | `repeat(Cat(), 2) = Cat(), Cat()` |
59+
| Arguments | Results | Example |
60+
| ---------------- | ---------------------------------------------- | --------------------------------- |
61+
| `item, [n=None]` | item, item, item, … endlessly or up to n times | `repeat(Cat(), 2) = Cat(), Cat()` |
6262

6363
``` py
6464
from itertools import repeat

itertools/terminated_iterators.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The `itertools` is a built-in module in Python 3 and is helpful for us to use a more pythonic way to manipulate the iterables (e.g., list, set, tuple, etc.).
44

5-
Another advantage of `itertools` is it also returns the [generator](../must_know/generator.md) type instances that have benefits of **lazy evaluation** from all its methods.
5+
Another advantage of `itertools` is it also returns the [generator](../must_know/generator.md) type instances that have benefits of **lazy evaluation** from all of its methods.
66

77
In this section, we will discuss the second part of `itertools`. The methods in this part all return a generator that generates finite items corresponding to your input.
88

@@ -28,9 +28,9 @@ In this section, we will discuss the second part of `itertools`. The methods in
2828

2929
This table takes [Python documentation #accumulate](https://docs.python.org/3/library/itertools.html#itertools.accumulate) as reference.
3030

31-
| Arguments | Results | Example |
32-
| ------------------------------------- | ------------------------------------ | --------------------------------- |
33-
| p (`iterable`), [, func=operator.add] | p[0], p[0]+p[1], p[0]+p[1]+p[2], ... | `accumulate([1, 2, 3]) = 1, 3, 6` |
31+
| Arguments | Results | Example |
32+
| ----------------------------------- | ------------------------------------ | --------------------------------- |
33+
| `p (iterable), [func=operator.add]` | p[0], p[0]+p[1], p[0]+p[1]+p[2], ... | `accumulate([1, 2, 3]) = 1, 3, 6` |
3434

3535
``` py
3636
import operator
@@ -50,7 +50,7 @@ This table takes [Python documentation #chain](https://docs.python.org/3/library
5050

5151
| Arguments | Results | Example |
5252
| ------------------ | -------------------------------------------- | -------------------------------- |
53-
| p, q (`iterables`) | p[0], p[1], ... p[-1], q[0], q[1], ... q[-1] | `chain("AB", "CD") = A, B, C, D` |
53+
| `p, q (iterables)` | p[0], p[1], ... p[-1], q[0], q[1], ... q[-1] | `chain("AB", "CD") = A, B, C, D` |
5454

5555
``` py
5656
from itertools import chain
@@ -69,7 +69,7 @@ This table takes [Python documentation #compress](https://docs.python.org/3/libr
6969

7070
| Arguments | Results | Example |
7171
| ------------------------- | ----------------------------------- | ---------------------------- |
72-
| p, selector (`iterables`) | (p[0] if s[0]), (p[1] if s[1]), ... | `compress("AB", [0, 1]) = B` |
72+
| `p, selector (iterables)` | (p[0] if s[0]), (p[1] if s[1]), ... | `compress("AB", [0, 1]) = B` |
7373

7474
``` py
7575
from itertools import compress
@@ -88,7 +88,7 @@ This table takes [Python documentation #filterfalse](https://docs.python.org/3/l
8888

8989
| Arguments | Results | Example |
9090
| -------------------- | ------------ | ------------------------------------------------- |
91-
| pred, p (`iterable`) | p[i] != pred | `filterfalse(lambda x: x%2==0, [1, 2, 3]) = 1, 3` |
91+
| `pred, p (iterable)` | p[i] != pred | `filterfalse(lambda x: x%2==0, [1, 2, 3]) = 1, 3` |
9292

9393
``` py
9494
from itertools import filterfalse
@@ -108,7 +108,7 @@ This table takes [Python documentation #groupby](https://docs.python.org/3/libra
108108

109109
| Arguments | Results | Tuples | Example |
110110
| ------------------------------- | --------------------------- | ---------------- | -------------------------------------------------------------- |
111-
| p (`iterable`)[key=lambda x: x] | tuple1, tuple2, tuple3, ... | (key, generator) | `groupby("AABBCCCAA") = [(A, AA), (B, BB), (C, CCC), (A, AA)]` |
111+
| `p (iterable)[key=lambda x: x]` | tuple1, tuple2, tuple3, ... | (key, generator) | `groupby("AABBCCCAA") = [(A, AA), (B, BB), (C, CCC), (A, AA)]` |
112112

113113
``` py
114114
from itertools import groupby
@@ -144,7 +144,7 @@ This table takes [Python documentation #islice](https://docs.python.org/3/librar
144144

145145
| Arguments | Results | Example |
146146
| ----------------------------------------------- | ----------------------------------- | ----------------------------------- |
147-
| p (`iterable`), [start=None], stop, [step=None] | p[start:stop:step] but in generator | `islice("ABCD", 0, None, 2) = A, C` |
147+
| `p (iterable), [start=None], stop, [step=None]` | p[start:stop:step] but in generator | `islice("ABCD", 0, None, 2) = A, C` |
148148

149149
``` py
150150
gen = islice([1, 2, 3], 2) # equals to A[:2]
@@ -167,7 +167,7 @@ This table takes [Python documentation #starmap](https://docs.python.org/3/libra
167167

168168
| Arguments | Results | Example |
169169
| -------------------- | ----------------------------- | ---------------------------------------- |
170-
| func, p (`iterable`) | func(*p[0]), ..., func(p[-1]) | `starmap(pow, [(2, 3), (2, 4)]) = 8, 16` |
170+
| `func, p (iterable)` | func(*p[0]), ..., func(p[-1]) | `starmap(pow, [(2, 3), (2, 4)]) = 8, 16` |
171171

172172
``` py
173173
from itertools import starmap
@@ -193,7 +193,7 @@ This table takes [Python documentation #takewhile](https://docs.python.org/3/lib
193193

194194
| Arguments | Results | Example |
195195
| -------------------- | -------------------------------- | ----------------------------------------------- |
196-
| pred, p (`iterable`) | p[0], p[1], ... until pred fails | `takewhile(lambda x: x<3, [1, 2, 3, 2]) = 1, 2` |
196+
| `pred, p (iterable)` | p[0], p[1], ... until pred fails | `takewhile(lambda x: x<3, [1, 2, 3, 2]) = 1, 2` |
197197

198198
``` py
199199
from itertools import takewhile
@@ -211,7 +211,7 @@ This table takes [Python documentation #dropwhile](https://docs.python.org/3/lib
211211

212212
| Arguments | Results | Example |
213213
| -------------------- | ------------------------------------------- | ----------------------------------------------- |
214-
| pred, p (`iterable`) | starting when pred fails, p[n], p[n+1], ... | `dropwhile(lambda x: x<3, [1, 2, 3, 2]) = 3, 2` |
214+
| `pred, p (iterable)` | starting when pred fails, p[n], p[n+1], ... | `dropwhile(lambda x: x<3, [1, 2, 3, 2]) = 3, 2` |
215215

216216
``` py
217217
gen = dropwhile(lambda x: x < 2, [1, 2, 3, 2, 1])
@@ -230,7 +230,7 @@ This table takes [Python documentation #zip_longest](https://docs.python.org/3/l
230230

231231
| Arguments | Results | Example |
232232
| ------------------------------------ | ------------------------------- | -------------------------------------------------------------------- |
233-
| p, q (`iterables`), [fillvalue=None] | (p[0], q[0]), (p[1], q[1]), ... | `zip_longest("ABC", [1, 2], fillvalue=-1) = (A, 1), (B, 2), (C, -1)` |
233+
| `p, q (iterables), [fillvalue=None]` | (p[0], q[0]), (p[1], q[1]), ... | `zip_longest("ABC", [1, 2], fillvalue=-1) = (A, 1), (B, 2), (C, -1)` |
234234

235235
``` py
236236
from itertools import zip_longest

0 commit comments

Comments
 (0)