@@ -206,19 +206,46 @@ Generator
206
206
** Convenient way to implement the iterator protocol.**
207
207
208
208
``` python
209
- def step (start , step_size ):
209
+ def count (start , step ):
210
210
while True :
211
211
yield start
212
- start += step_size
212
+ start += step
213
213
```
214
214
215
215
``` 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 )
218
218
(10 , 12 , 14 )
219
219
```
220
220
221
221
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
+
222
249
Type
223
250
----
224
251
``` python
@@ -427,6 +454,39 @@ shuffle(<list>)
427
454
```
428
455
429
456
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
+
430
490
Datetime
431
491
--------
432
492
``` python
@@ -1338,79 +1398,6 @@ Hashlib
1338
1398
```
1339
1399
1340
1400
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
-
1414
1401
Introspection and Metaprograming
1415
1402
--------------------------------
1416
1403
** Inspecting code at runtime and code that generates code. You can:**
0 commit comments