-
Notifications
You must be signed in to change notification settings - Fork 0
Lecture 5
-
itertools.accumulate(iterable[, func])
-
itertools.chain(*iterables)
my_list = list(chain(['foo', 'bar'], cmd, numbers)) -
itertools.chain.from_iterable(iterable)
list(chain.from_iterable([cmd, numbers])) -
itertools.compress(data, selectors)
letters = 'ABCDEFG'
bools = [True, False, True, True, False]
print(list(compress(letters, bools))) - itertools.dropwhile(predicate, iterable)
def less_than_five(x):
return x < 5
print(list(dropwhile(less_than_five, [1, 4, 5, 1, 6])))
print(list(dropwhile(lambda x: x > 5, [6, 7, 8, 9, 1, 2, 3, 10])))-
itertools.filterfalse(predicate, iterable)
-
itertools.groupby(iterable, key=None)
The key argument is a function, or if none is specified, defaults to the identity function which returns the element unchanged
from itertools import groupby
vehicles = [('Ford', 'Taurus'), ('Dodge', 'Durango'),
('Chevrolet', 'Cobalt'), ('Ford', 'F150'),
('Dodge', 'Charger'), ('Ford', 'GT')]
sorted_vehicles = sorted(vehicles)
for key, group in groupby(sorted_vehicles, lambda make: make[0]):
for make, model in group:
print('{model} is made by {make}'.format(model=model, make=make))
print ("**** END OF GROUP ***\n")-
itertools.islice(iterable, start, stop[, step])
-
itertools.starmap(function, iterable) ?????????
The difference between map() and starmap() parallels the distinction between function(a,b) and function(*c).
-
Defining functions with *args
-
itertools.takewhile(predicate, iterable)
opposite of the dropwhile iterator
-
itertools.tee(iterable, n=2)
-
itertools.zip_longest(*iterables, fillvalue=None)
If the iterables don’t happen to be the same length, then you can also pass in a fillvalue
-
itertools.combinations(iterable, r)
-
itertools.combinations_with_replacement(iterable, r)
-
itertools.product(*iterables, repeat=1)
-
itertools.permutations(iterable, r=None)
-
Calling functions with *args