Skip to content

Commit 41c365c

Browse files
committed
[change] terminated iterators
1 parent ba69668 commit 41c365c

File tree

2 files changed

+183
-34
lines changed

2 files changed

+183
-34
lines changed

README.md

Lines changed: 112 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -645,71 +645,171 @@ for cat in gen:
645645
### accumulate
646646

647647
``` py
648+
import operator
649+
from itertools import accumulate
650+
651+
gen = accumulate([1, 2, 3, 4])
652+
list(gen) # [1, 3, 6, 10]
653+
654+
655+
gen = accumulate([1, 2, 3, 4], func=operator.mul)
656+
list(gen) # [1, 2, 6, 24]
648657
```
649658

650659
### chain
651660

652-
``` py
661+
```py
662+
from itertools import chain
663+
664+
gen = chain([1, 2], [3, 4])
665+
list(gen) # [1, 2, 3, 4]
666+
667+
668+
gen = chain("AB", "CD")
669+
list(gen) # [A, B, C, D]
653670
```
654671

655672
### compress
656673

657-
``` py
674+
```py
675+
from itertools import compress
676+
677+
gen = compress([1, 2, 3], [1, 0, 1])
678+
gen = compress([1, 2, 3], [True, False, True]) # same
679+
680+
list(gen) # [1, 3]
658681
```
659682

660683
### filterfalse
661684

662-
``` py
685+
```py
686+
from itertools import filterfalse
687+
688+
gen = filterfalse(lambda x: x%2 == 0, [1, 2, 3])
689+
690+
list(gen) # [1, 3]
663691
```
664692

665693
### groupby
666694

667-
``` py
695+
```py
696+
from itertools import groupby
697+
698+
gen = groupby("AABBCCCAA") # default func = lambda x: x
699+
for k, g in gen:
700+
print(k, list(g))
701+
# A [A, A]
702+
# B [B, B]
703+
# C [C, C, C]
704+
# A [A, A]
705+
706+
707+
gen = groupby([1, 2, 3, 4], lambda x: x // 3)
708+
for k, g in gen:
709+
print(k, list(g))
710+
# 0 [1, 2]
711+
# 1 [3, 4]
712+
713+
714+
gen = groupby([("A", 100), ("B", 200), ("C", 600)], lambda x: x[1] > 500)
715+
for k, g in gen:
716+
print(k, list(g))
717+
# False [(A, 100), (B, 200)]
718+
# True [(C, 600)]
668719
```
669720

670721
### islice
671722

672-
``` py
723+
```py
724+
gen = islice([1, 2, 3], 2) # equals to A[:2]
725+
list(gen) # [1, 2]
726+
727+
728+
gen = islice("ABCD", 2, 4) # equals to A[2:4]
729+
list(gen) # [C, D]
730+
731+
732+
gen = islice("ABCD", 0, None, 2) # equals to A[::2]
733+
list(gen) # [A, C]
673734
```
674735

675736
### starmap
676737

677-
``` py
738+
```py
739+
from itertools import starmap
740+
741+
# with only one argument
742+
gen = starmap(lambda x: x.lower(), "ABCD")
743+
list(gen) # [a, b, c, d]
744+
745+
746+
# with 2 arguments
747+
gen = starmap(lambda x, y: x + y, [(1, 2), (3, 4)])
748+
list(gen) # [3, 7]
749+
750+
751+
# with different size of arugments
752+
gen = starmap(lambda *keys: sum(keys) / len(keys), [[3, 8, 3], [4, 2]])
753+
list(gen) # [4.6666667, 3.0]
678754
```
679755

680756
### takewhile
681757

682-
``` py
758+
```py
759+
from itertools import takewhile
760+
761+
gen = takewhile(lambda x: x < 2, [1, 2, 3, 2, 1])
762+
list(gen) # [1]
763+
764+
gen = takewhile(lambda x: x.isupper(), "ABCdefgHIJ")
765+
list(gen) # [A, B, C]
683766
```
684767

685768
### dropwhile
686769

687-
``` py
770+
```py
771+
gen = dropwhile(lambda x: x < 2, [1, 2, 3, 2, 1])
772+
list(gen) # [2, 3, 2, 1]
773+
774+
775+
gen = dropwhile(lambda x: x.isupper(), "ABCdefgHIJ")
776+
list(gen) # [d, e, f, g, H, I, J]
688777
```
689778

690779
### zip_longest
691780

692-
``` py
781+
```py
782+
from itertools import zip_longest
783+
784+
gen = zip_longest("ABC", ("X", "Y"))
785+
list(gen) # [('A', 'X'), ('B', 'Y'), ('C', None)]
786+
787+
788+
gen = zip_longest("ABC", [1, 2], fillvalue=-1)
789+
list(gen) # [('A', 1), ('B', 2), ('C', -1)]
693790
```
694791

695792
## [Combinatoric iterators](itertools/combinatoric_iterators.md)
696793

697794
### product
698795

699796

700-
``` py
797+
```py
798+
701799
```
702800

703801
### permutations
704802

705803

706-
``` py
804+
```py
805+
707806
```
708807

709808
### combinations
710809

711810

712-
``` py
811+
```py
812+
713813
```
714814

715815
## os

itertools/terminated_iterators.md

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ In this section, we will discuss the second part of `itertools`. The methods in
2424

2525
## accumulate
2626

27-
`accumulate` works like the [reduce](../must_know/reduce.md), but `accumulate` keep tracking the process of calculation and return in a generator.
27+
`accumulate` works like [reduce](../must_know/reduce.md), but `accumulate` keep tracking the process of calculation and return in a generator.
2828

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

@@ -93,26 +93,52 @@ This table takes [Python documentation #filterfalse](https://docs.python.org/3/l
9393
``` py
9494
from itertools import filterfalse
9595

96-
gen = filterfalse(lambda x: x%2 ==0, [1, 2, 3])
96+
gen = filterfalse(lambda x: x%2 == 0, [1, 2, 3])
9797

9898
list(gen) # [1, 3]
9999
```
100100

101101
## groupby
102102

103+
`groupby` will separate the iterable (e.g., `list`, `dict`, `tuple`) into `key-generator pairs` based on the `keyfunc` (default is `labmda x: x`).
104+
105+
It is important to note that it is **not like the "groupby" in SQL** where the data is sorted before the group; If you want to perfectly separate the iterable, you should sort it before `groupby`.
106+
103107
This table takes [Python documentation #groupby](https://docs.python.org/3/library/itertools.html#itertools.groupby) as reference.
104108

105-
| Arguments | Results | Example |
106-
| ----------------------- | ------- | ------- |
107-
| p (`iterable`), [, key] |
109+
| Arguments | Results | Tuples | Example |
110+
| ------------------------------- | --------------------------- | ---------------- | -------------------------------------------------------------- |
111+
| p (`iterable`)[key=lambda x: x] | tuple1, tuple2, tuple3, ... | (key, generator) | `groupby("AABBCCCAA") = [(A, AA), (B, BB), (C, CCC), (A, AA)]` |
108112

109113
``` py
114+
from itertools import groupby
115+
116+
gen = groupby("AABBCCCAA") # default func = lambda x: x
117+
for k, g in gen:
118+
print(k, list(g))
119+
# A [A, A]
120+
# B [B, B]
121+
# C [C, C, C]
122+
# A [A, A]
123+
110124

125+
gen = groupby([1, 2, 3, 4], lambda x: x // 3)
126+
for k, g in gen:
127+
print(k, list(g))
128+
# 0 [1, 2]
129+
# 1 [3, 4]
130+
131+
132+
gen = groupby([("A", 100), ("B", 200), ("C", 600)], lambda x: x[1] > 500)
133+
for k, g in gen:
134+
print(k, list(g))
135+
# False [(A, 100), (B, 200)]
136+
# True [(C, 600)]
111137
```
112138

113139
## islice
114140

115-
<!-- just like slice function in list but islice returns generator -->
141+
The function of `islice` is the same as the `slice function` in the list (e.g., list[0:4:2]), the only difference is it returns a generator.
116142

117143
This table takes [Python documentation #islice](https://docs.python.org/3/library/itertools.html#itertools.islice) as reference.
118144

@@ -121,26 +147,26 @@ This table takes [Python documentation #islice](https://docs.python.org/3/librar
121147
| p (`iterable`), [start=None], stop, [step=None] | p[start:stop:step] but in generator | `islice("ABCD", 0, None, 2) = A, C` |
122148

123149
``` py
124-
gen = islice([1, 2, 3], 2)
150+
gen = islice([1, 2, 3], 2) # equals to A[:2]
125151
list(gen) # [1, 2]
126152

127153

128-
gen = islice("ABCD", 2, 4)
154+
gen = islice("ABCD", 2, 4) # equals to A[2:4]
129155
list(gen) # [C, D]
130156

131157

132-
gen = islice("ABCD", 0, None, 2)
158+
gen = islice("ABCD", 0, None, 2) # equals to A[::2]
133159
list(gen) # [A, C]
134160
```
135161

136162
## starmap
137163

138-
<!-- just like map function but it directly use the elements as func arguments -->
164+
The function of `starmap` is almost the same as [map](../must_know/map.md), except that `starmap` treats elements directly as func arguments in the same order.
139165

140166
This table takes [Python documentation #starmap](https://docs.python.org/3/library/itertools.html#itertools.starmap) as reference.
141167

142-
| Arguments | Results | Example |
143-
| -------------------- | ----------------------------- | ---------------------------- |
168+
| Arguments | Results | Example |
169+
| -------------------- | ----------------------------- | ---------------------------------------- |
144170
| func, p (`iterable`) | func(*p[0]), ..., func(p[-1]) | `starmap(pow, [(2, 3), (2, 4)]) = 8, 16` |
145171

146172
``` py
@@ -150,10 +176,12 @@ from itertools import starmap
150176
gen = starmap(lambda x: x.lower(), "ABCD")
151177
list(gen) # [a, b, c, d]
152178

179+
153180
# with 2 arguments
154181
gen = starmap(lambda x, y: x + y, [(1, 2), (3, 4)])
155182
list(gen) # [3, 7]
156183

184+
157185
# with different size of arugments
158186
gen = starmap(lambda *keys: sum(keys) / len(keys), [[3, 8, 3], [4, 2]])
159187
list(gen) # [4.6666667, 3.0]
@@ -163,36 +191,56 @@ list(gen) # [4.6666667, 3.0]
163191

164192
This table takes [Python documentation #takewhile](https://docs.python.org/3/library/itertools.html#itertools.takewhile) as reference.
165193

166-
| Arguments | Results | Example |
167-
| --------- | ------- | ------- |
168-
194+
| Arguments | Results | Example |
195+
| -------------------- | -------------------------------- | ----------------------------------------------- |
196+
| pred, p (`iterable`) | p[0], p[1], ... until pred fails | `takewhile(lambda x: x<3, [1, 2, 3, 2]) = 1, 2` |
169197

170198
``` py
199+
from itertools import takewhile
200+
201+
gen = takewhile(lambda x: x < 2, [1, 2, 3, 2, 1])
202+
list(gen) # [1]
171203

204+
gen = takewhile(lambda x: x.isupper(), "ABCdefgHIJ")
205+
list(gen) # [A, B, C]
172206
```
173207

174208
## dropwhile
175209

176210
This table takes [Python documentation #dropwhile](https://docs.python.org/3/library/itertools.html#itertools.dropwhile) as reference.
177211

178-
| Arguments | Results | Example |
179-
| --------- | ------- | ------- |
180-
212+
| Arguments | Results | Example |
213+
| -------------------- | ------------------------------------------- | ----------------------------------------------- |
214+
| pred, p (`iterable`) | starting when pred fails, p[n], p[n+1], ... | `dropwhile(lambda x: x<3, [1, 2, 3, 2]) = 3, 2` |
181215

182216
``` py
217+
gen = dropwhile(lambda x: x < 2, [1, 2, 3, 2, 1])
218+
list(gen) # [2, 3, 2, 1]
219+
183220

221+
gen = dropwhile(lambda x: x.isupper(), "ABCdefgHIJ")
222+
list(gen) # [d, e, f, g, H, I, J]
184223
```
185224

186225
## zip_longest
187226

188-
This table takes [Python documentation #zip_longest](https://docs.python.org/3/library/itertools.html#itertools.zip_longest) as reference.
227+
The function of `zip_longest` is almost the same as [zip](../must_know/zip.md), except that `zip_longest` will fill the missing value with `fillvalue` (default is `None`) if the iterables are with uneven length.
189228

190-
| Arguments | Results | Example |
191-
| --------- | ------- | ------- |
229+
This table takes [Python documentation #zip_longest](https://docs.python.org/3/library/itertools.html#itertools.zip_longest) as reference.
192230

231+
| Arguments | Results | Example |
232+
| ------------------------------------ | ------------------------------- | -------------------------------------------------------------------- |
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)` |
193234

194235
``` py
236+
from itertools import zip_longest
237+
238+
gen = zip_longest("ABC", ("X", "Y"))
239+
list(gen) # [('A', 'X'), ('B', 'Y'), ('C', None)]
240+
195241

242+
gen = zip_longest("ABC", [1, 2], fillvalue=-1)
243+
list(gen) # [('A', 1), ('B', 2), ('C', -1)]
196244
```
197245

198246

@@ -203,4 +251,5 @@ This table takes [Python documentation #zip_longest](https://docs.python.org/3/l
203251
| Python documentation - itertools | https://docs.python.org/3/library/itertools.html |
204252
| Python Tutorial: Itertools Module - Iterator Functions for Efficient Looping | https://www.youtube.com/watch?v=Qu3dThVy6KQ |
205253
| Python 好用模組介紹 - itertools & more-itertools | https://myapollo.com.tw/zh-tw/python-itertools-more-itertools/ |
206-
| Python標準庫之itertools庫的使用方法 | https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/364249/ |
254+
| Python標準庫之itertools庫的使用方法 | https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/364249/ |
255+
| python groupby 筆記 | http://ot-note.logdown.com/posts/1379449 |

0 commit comments

Comments
 (0)