@@ -775,10 +775,7 @@ which incur interpreter overhead.
775
775
return sum(map(pred, iterable))
776
776
777
777
def pad_none(iterable):
778
- """Returns the sequence elements and then returns None indefinitely.
779
-
780
- Useful for emulating the behavior of the built-in map() function.
781
- """
778
+ "Returns the sequence elements and then returns None indefinitely."
782
779
return chain(iterable, repeat(None))
783
780
784
781
def ncycles(iterable, n):
@@ -850,6 +847,13 @@ which incur interpreter overhead.
850
847
else:
851
848
raise ValueError('Expected fill, strict, or ignore')
852
849
850
+ def batched(iterable, n):
851
+ "Batch data into lists of length n. The last batch may be shorter."
852
+ # batched('ABCDEFG', 3) --> ABC DEF G
853
+ it = iter(iterable)
854
+ while (batch := list(islice(it, n))):
855
+ yield batch
856
+
853
857
def triplewise(iterable):
854
858
"Return overlapping triplets from an iterable"
855
859
# triplewise('ABCDEFG') --> ABC BCD CDE DEF EFG
@@ -1168,8 +1172,8 @@ which incur interpreter overhead.
1168
1172
1169
1173
>>> list (sieve(30 ))
1170
1174
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
1171
- >>> small_primes = [2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 , 53 , 59 ]
1172
- >>> all (list (sieve(n)) == [p for p in small_primes if p < n] for n in range (60 ))
1175
+ >>> small_primes = [2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 , 53 , 59 , 61 , 67 , 71 , 73 , 79 , 83 , 89 , 97 ]
1176
+ >>> all (list (sieve(n)) == [p for p in small_primes if p < n] for n in range (101 ))
1173
1177
True
1174
1178
>>> len (list (sieve(100 )))
1175
1179
25
@@ -1212,6 +1216,36 @@ which incur interpreter overhead.
1212
1216
>>> list (grouper(' abcdefg' , n = 3 , incomplete = ' ignore' ))
1213
1217
[('a', 'b', 'c'), ('d', 'e', 'f')]
1214
1218
1219
+ >>> list (batched(' ABCDEFG' , 3 ))
1220
+ [['A', 'B', 'C'], ['D', 'E', 'F'], ['G']]
1221
+ >>> list (batched(' ABCDEF' , 3 ))
1222
+ [['A', 'B', 'C'], ['D', 'E', 'F']]
1223
+ >>> list (batched(' ABCDE' , 3 ))
1224
+ [['A', 'B', 'C'], ['D', 'E']]
1225
+ >>> list (batched(' ABCD' , 3 ))
1226
+ [['A', 'B', 'C'], ['D']]
1227
+ >>> list (batched(' ABC' , 3 ))
1228
+ [['A', 'B', 'C']]
1229
+ >>> list (batched(' AB' , 3 ))
1230
+ [['A', 'B']]
1231
+ >>> list (batched(' A' , 3 ))
1232
+ [['A']]
1233
+ >>> list (batched(' ' , 3 ))
1234
+ []
1235
+ >>> list (batched(' ABCDEFG' , 2 ))
1236
+ [['A', 'B'], ['C', 'D'], ['E', 'F'], ['G']]
1237
+ >>> list (batched(' ABCDEFG' , 1 ))
1238
+ [['A'], ['B'], ['C'], ['D'], ['E'], ['F'], ['G']]
1239
+ >>> list (batched(' ABCDEFG' , 0 ))
1240
+ []
1241
+ >>> list (batched(' ABCDEFG' , - 1 ))
1242
+ Traceback (most recent call last):
1243
+ ...
1244
+ ValueError: Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize.
1245
+ >>> s = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1246
+ >>> all (list (flatten(batched(s[:n], 5 ))) == list (s[:n]) for n in range (len (s)))
1247
+ True
1248
+
1215
1249
>>> list (triplewise(' ABCDEFG' ))
1216
1250
[('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E'), ('D', 'E', 'F'), ('E', 'F', 'G')]
1217
1251
0 commit comments