Created
April 22, 2017 11:51
-
-
Save slchangtw/9d52a643264afbe47175d07dcaf5d3fc to your computer and use it in GitHub Desktop.
itertools (python)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!usr/local/env python3 | |
# -*- coding: utf-8 -*- | |
import itertools | |
def consonant(c): | |
return c.lower() not in "aeiou" | |
# (built-in) filter() | |
list(filter(consonant, 'Aartttik')) | |
list(itertools.filterfalse(consonant, 'Aartttik')) | |
# takewhile() returns the iterable until predicate(element) is False | |
list(itertools.takewhile(consonant, 'rrrAartttik')) | |
# ['r', 'r', 'r'] | |
list(itertools.dropwhile(consonant, 'rrrAartttik')) | |
# ['A', 'a', 'r', 't', 't', 't', 'i', 'k'] | |
list(itertools.islice('Aartttik', 1, 3)) | |
# ['a', 'r'] | |
# accumulate(it, fun) | |
list(itertools.accumulate([8, 3, 5, 7])) | |
# [8, 11, 16, 23] | |
list(itertools.accumulate([8, 3, 5, 7], max)) | |
# [8, 8, 8, 8] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment