1186
1186
[25, 26, 27, 28]
1188
>>> def take(n, seq):
1189
... return list(islice(seq, n))
1191
>>> def enumerate(iterable):
1192
... return izip(count(), iterable)
1194
>>> def tabulate(function):
1188
>>> def take(n, iterable):
1189
... "Return first n items of the iterable as a list"
1190
... return list(islice(iterable, n))
1192
>>> def enumerate(iterable, start=0):
1193
... return izip(count(start), iterable)
1195
>>> def tabulate(function, start=0):
1195
1196
... "Return function(0), function(1), ..."
1196
... return imap(function, count())
1198
>>> def iteritems(mapping):
1199
... return izip(mapping.iterkeys(), mapping.itervalues())
1197
... return imap(function, count(start))
1201
1199
>>> def nth(iterable, n):
1202
... "Returns the nth item"
1200
... "Returns the nth item or empty list"
1203
1201
... return list(islice(iterable, n, n+1))
1205
>>> def all(seq, pred=None):
1206
... "Returns True if pred(x) is true for every element in the iterable"
1207
... for elem in ifilterfalse(pred, seq):
1211
>>> def any(seq, pred=None):
1212
... "Returns True if pred(x) is true for at least one element in the iterable"
1213
... for elem in ifilter(pred, seq):
1217
>>> def no(seq, pred=None):
1218
... "Returns True if pred(x) is false for every element in the iterable"
1219
... for elem in ifilter(pred, seq):
1223
>>> def quantify(seq, pred=None):
1224
... "Count how many times the predicate is true in the sequence"
1225
... return sum(imap(pred, seq))
1227
>>> def padnone(seq):
1203
>>> def quantify(iterable, pred=bool):
1204
... "Count how many times the predicate is true"
1205
... return sum(imap(pred, iterable))
1207
>>> def padnone(iterable):
1228
1208
... "Returns the sequence elements and then returns None indefinitely"
1229
... return chain(seq, repeat(None))
1209
... return chain(iterable, repeat(None))
1231
>>> def ncycles(seq, n):
1232
... "Returns the sequence elements n times"
1233
... return chain(*repeat(seq, n))
1211
>>> def ncycles(iterable, n):
1212
... "Returns the seqeuence elements n times"
1213
... return chain(*repeat(iterable, n))
1235
1215
>>> def dotproduct(vec1, vec2):
1236
1216
... return sum(imap(operator.mul, vec1, vec2))
1254
1234
... return izip(a, b)
1256
1236
>>> def grouper(n, iterable, fillvalue=None):
1257
... "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
1237
... "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
1258
1238
... args = [iter(iterable)] * n
1259
... kwds = dict(fillvalue=fillvalue)
1260
... return izip_longest(*args, **kwds)
1239
... return izip_longest(fillvalue=fillvalue, *args)
1262
1241
>>> def roundrobin(*iterables):
1263
... "roundrobin('abc', 'd', 'ef') --> 'a', 'd', 'e', 'b', 'f', 'c'"
1242
... "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
1264
1243
... # Recipe credited to George Sakkis
1265
1244
... pending = len(iterables)
1266
1245
... nexts = cycle(iter(it).next for it in iterables)
1280
1259
... yield set(x for m, x in pairs if m&n)
1282
1261
>>> def compress(data, selectors):
1283
... "compress('abcdef', [1,0,1,0,1,1]) --> a c e f"
1284
... for d, s in izip(data, selectors):
1262
... "compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F"
1263
... return (d for d, s in izip(data, selectors) if s)
1265
>>> def combinations_with_replacement(iterable, r):
1266
... "combinations_with_replacement('ABC', 3) --> AA AB AC BB BC CC"
1267
... pool = tuple(iterable)
1269
... indices = [0] * r
1270
... yield tuple(pool[i] for i in indices)
1272
... for i in reversed(range(r)):
1273
... if indices[i] != n - 1:
1277
... indices[i:] = [indices[i] + 1] * (r - i)
1278
... yield tuple(pool[i] for i in indices)
1288
1280
This is not part of the examples but it tests to make sure the definitions
1289
1281
perform as purported.
1300
1292
>>> nth('abcde', 3)
1303
>>> all([2, 4, 6, 8], lambda x: x%2==0)
1306
>>> all([2, 3, 6, 8], lambda x: x%2==0)
1309
>>> any([2, 4, 6, 8], lambda x: x%2==0)
1312
>>> any([1, 3, 5, 9], lambda x: x%2==0,)
1315
>>> no([1, 3, 5, 9], lambda x: x%2==0)
1318
>>> no([1, 2, 5, 9], lambda x: x%2==0)
1321
1295
>>> quantify(xrange(99), lambda x: x%2==0)
1362
1336
>>> list(compress('abcdef', [1,0,1,0,1,1]))
1363
1337
['a', 'c', 'e', 'f']
1339
>>> list(combinations_with_replacement('abc', 2))
1340
[('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'b'), ('b', 'c'), ('c', 'c')]
1367
1344
__test__ = {'libreftest' : libreftest}