You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: itertools/terminated_iterators.md
+71-22Lines changed: 71 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ In this section, we will discuss the second part of `itertools`. The methods in
24
24
25
25
## accumulate
26
26
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.
28
28
29
29
This table takes [Python documentation #accumulate](https://docs.python.org/3/library/itertools.html#itertools.accumulate) as reference.
30
30
@@ -93,26 +93,52 @@ This table takes [Python documentation #filterfalse](https://docs.python.org/3/l
93
93
```py
94
94
from itertools import filterfalse
95
95
96
-
gen = filterfalse(lambdax: x%2==0, [1, 2, 3])
96
+
gen = filterfalse(lambdax: x%2==0, [1, 2, 3])
97
97
98
98
list(gen) # [1, 3]
99
99
```
100
100
101
101
## groupby
102
102
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
+
103
107
This table takes [Python documentation #groupby](https://docs.python.org/3/library/itertools.html#itertools.groupby) as reference.
<!-- 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.
116
142
117
143
This table takes [Python documentation #islice](https://docs.python.org/3/library/itertools.html#itertools.islice) as reference.
118
144
@@ -121,26 +147,26 @@ This table takes [Python documentation #islice](https://docs.python.org/3/librar
121
147
| p (`iterable`), [start=None], stop, [step=None]| p[start:stop:step] but in generator |`islice("ABCD", 0, None, 2) = A, C`|
122
148
123
149
```py
124
-
gen = islice([1, 2, 3], 2)
150
+
gen = islice([1, 2, 3], 2)# equals to A[:2]
125
151
list(gen) # [1, 2]
126
152
127
153
128
-
gen = islice("ABCD", 2, 4)
154
+
gen = islice("ABCD", 2, 4)# equals to A[2:4]
129
155
list(gen) # [C, D]
130
156
131
157
132
-
gen = islice("ABCD", 0, None, 2)
158
+
gen = islice("ABCD", 0, None, 2)# equals to A[::2]
133
159
list(gen) # [A, C]
134
160
```
135
161
136
162
## starmap
137
163
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.
139
165
140
166
This table takes [Python documentation #starmap](https://docs.python.org/3/library/itertools.html#itertools.starmap) as reference.
| pred, p (`iterable`) | starting when pred fails, p[n], p[n+1], ... |`dropwhile(lambda x: x<3, [1, 2, 3, 2]) = 3, 2`|
181
215
182
216
```py
217
+
gen = dropwhile(lambdax: x <2, [1, 2, 3, 2, 1])
218
+
list(gen) # [2, 3, 2, 1]
219
+
183
220
221
+
gen = dropwhile(lambdax: x.isupper(), "ABCdefgHIJ")
222
+
list(gen) # [d, e, f, g, H, I, J]
184
223
```
185
224
186
225
## zip_longest
187
226
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.
189
228
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.
0 commit comments