Skip to content

Instantly share code, notes, and snippets.

@slchangtw
Created April 22, 2017 11:51
Show Gist options
  • Save slchangtw/9d52a643264afbe47175d07dcaf5d3fc to your computer and use it in GitHub Desktop.
Save slchangtw/9d52a643264afbe47175d07dcaf5d3fc to your computer and use it in GitHub Desktop.
itertools (python)
#!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