Created
May 17, 2016 10:09
-
-
Save mr1azl/b99050d6d034a27cc6ac0bc8b5dac945 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "def take(n, iterable):\n", | |
| " \"Return first n items of the iterable as a list\"\n", | |
| " return list(islice(iterable, n))\n", | |
| "\n", | |
| "def tabulate(function, start=0):\n", | |
| " \"Return function(0), function(1), ...\"\n", | |
| " return imap(function, count(start))\n", | |
| "\n", | |
| "def consume(iterator, n):\n", | |
| " \"Advance the iterator n-steps ahead. If n is none, consume entirely.\"\n", | |
| " # Use functions that consume iterators at C speed.\n", | |
| " if n is None:\n", | |
| " # feed the entire iterator into a zero-length deque\n", | |
| " collections.deque(iterator, maxlen=0)\n", | |
| " else:\n", | |
| " # advance to the empty slice starting at position n\n", | |
| " next(islice(iterator, n, n), None)\n", | |
| "\n", | |
| "def nth(iterable, n, default=None):\n", | |
| " \"Returns the nth item or a default value\"\n", | |
| " return next(islice(iterable, n, None), default)\n", | |
| "\n", | |
| "def all_equal(iterable):\n", | |
| " \"Returns True if all the elements are equal to each other\"\n", | |
| " g = groupby(iterable)\n", | |
| " return next(g, True) and not next(g, False)\n", | |
| "\n", | |
| "def quantify(iterable, pred=bool):\n", | |
| " \"Count how many times the predicate is true\"\n", | |
| " return sum(imap(pred, iterable))\n", | |
| "\n", | |
| "def padnone(iterable):\n", | |
| " \"\"\"Returns the sequence elements and then returns None indefinitely.\n", | |
| "\n", | |
| " Useful for emulating the behavior of the built-in map() function.\n", | |
| " \"\"\"\n", | |
| " return chain(iterable, repeat(None))\n", | |
| "\n", | |
| "def ncycles(iterable, n):\n", | |
| " \"Returns the sequence elements n times\"\n", | |
| " return chain.from_iterable(repeat(tuple(iterable), n))\n", | |
| "\n", | |
| "def dotproduct(vec1, vec2):\n", | |
| " return sum(imap(operator.mul, vec1, vec2))\n", | |
| "\n", | |
| "def flatten(listOfLists):\n", | |
| " \"Flatten one level of nesting\"\n", | |
| " return chain.from_iterable(listOfLists)\n", | |
| "\n", | |
| "def repeatfunc(func, times=None, *args):\n", | |
| " \"\"\"Repeat calls to func with specified arguments.\n", | |
| "\n", | |
| " Example: repeatfunc(random.random)\n", | |
| " \"\"\"\n", | |
| " if times is None:\n", | |
| " return starmap(func, repeat(args))\n", | |
| " return starmap(func, repeat(args, times))\n", | |
| "\n", | |
| "def pairwise(iterable):\n", | |
| " \"s -> (s0,s1), (s1,s2), (s2, s3), ...\"\n", | |
| " a, b = tee(iterable)\n", | |
| " next(b, None)\n", | |
| " return izip(a, b)\n", | |
| "\n", | |
| "def grouper(iterable, n, fillvalue=None):\n", | |
| " \"Collect data into fixed-length chunks or blocks\"\n", | |
| " # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx\n", | |
| " args = [iter(iterable)] * n\n", | |
| " return izip_longest(fillvalue=fillvalue, *args)\n", | |
| "\n", | |
| "def roundrobin(*iterables):\n", | |
| " \"roundrobin('ABC', 'D', 'EF') --> A D E B F C\"\n", | |
| " # Recipe credited to George Sakkis\n", | |
| " pending = len(iterables)\n", | |
| " nexts = cycle(iter(it).next for it in iterables)\n", | |
| " while pending:\n", | |
| " try:\n", | |
| " for next in nexts:\n", | |
| " yield next()\n", | |
| " except StopIteration:\n", | |
| " pending -= 1\n", | |
| " nexts = cycle(islice(nexts, pending))\n", | |
| "\n", | |
| "def powerset(iterable):\n", | |
| " \"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)\"\n", | |
| " s = list(iterable)\n", | |
| " return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))\n", | |
| "\n", | |
| "def unique_everseen(iterable, key=None):\n", | |
| " \"List unique elements, preserving order. Remember all elements ever seen.\"\n", | |
| " # unique_everseen('AAAABBBCCDAABBB') --> A B C D\n", | |
| " # unique_everseen('ABBCcAD', str.lower) --> A B C D\n", | |
| " seen = set()\n", | |
| " seen_add = seen.add\n", | |
| " if key is None:\n", | |
| " for element in ifilterfalse(seen.__contains__, iterable):\n", | |
| " seen_add(element)\n", | |
| " yield element\n", | |
| " else:\n", | |
| " for element in iterable:\n", | |
| " k = key(element)\n", | |
| " if k not in seen:\n", | |
| " seen_add(k)\n", | |
| " yield element\n", | |
| "\n", | |
| "def unique_justseen(iterable, key=None):\n", | |
| " \"List unique elements, preserving order. Remember only the element just seen.\"\n", | |
| " # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B\n", | |
| " # unique_justseen('ABBCcAD', str.lower) --> A B C A D\n", | |
| " return imap(next, imap(itemgetter(1), groupby(iterable, key)))\n", | |
| "\n", | |
| "def iter_except(func, exception, first=None):\n", | |
| " \"\"\" Call a function repeatedly until an exception is raised.\n", | |
| "\n", | |
| " Converts a call-until-exception interface to an iterator interface.\n", | |
| " Like __builtin__.iter(func, sentinel) but uses an exception instead\n", | |
| " of a sentinel to end the loop.\n", | |
| "\n", | |
| " Examples:\n", | |
| " bsddbiter = iter_except(db.next, bsddb.error, db.first)\n", | |
| " heapiter = iter_except(functools.partial(heappop, h), IndexError)\n", | |
| " dictiter = iter_except(d.popitem, KeyError)\n", | |
| " dequeiter = iter_except(d.popleft, IndexError)\n", | |
| " queueiter = iter_except(q.get_nowait, Queue.Empty)\n", | |
| " setiter = iter_except(s.pop, KeyError)\n", | |
| "\n", | |
| " \"\"\"\n", | |
| " try:\n", | |
| " if first is not None:\n", | |
| " yield first()\n", | |
| " while 1:\n", | |
| " yield func()\n", | |
| " except exception:\n", | |
| " pass\n", | |
| "\n", | |
| "def random_product(*args, **kwds):\n", | |
| " \"Random selection from itertools.product(*args, **kwds)\"\n", | |
| " pools = map(tuple, args) * kwds.get('repeat', 1)\n", | |
| " return tuple(random.choice(pool) for pool in pools)\n", | |
| "\n", | |
| "def random_permutation(iterable, r=None):\n", | |
| " \"Random selection from itertools.permutations(iterable, r)\"\n", | |
| " pool = tuple(iterable)\n", | |
| " r = len(pool) if r is None else r\n", | |
| " return tuple(random.sample(pool, r))\n", | |
| "\n", | |
| "def random_combination(iterable, r):\n", | |
| " \"Random selection from itertools.combinations(iterable, r)\"\n", | |
| " pool = tuple(iterable)\n", | |
| " n = len(pool)\n", | |
| " indices = sorted(random.sample(xrange(n), r))\n", | |
| " return tuple(pool[i] for i in indices)\n", | |
| "\n", | |
| "def random_combination_with_replacement(iterable, r):\n", | |
| " \"Random selection from itertools.combinations_with_replacement(iterable, r)\"\n", | |
| " pool = tuple(iterable)\n", | |
| " n = len(pool)\n", | |
| " indices = sorted(random.randrange(n) for i in xrange(r))\n", | |
| " return tuple(pool[i] for i in indices)\n", | |
| "\n", | |
| "def tee_lookahead(t, i):\n", | |
| " \"\"\"Inspect the i-th upcomping value from a tee object\n", | |
| " while leaving the tee object at its current position.\n", | |
| "\n", | |
| " Raise an IndexError if the underlying iterator doesn't\n", | |
| " have enough values.\n", | |
| "\n", | |
| " \"\"\"\n", | |
| " for value in islice(t.__copy__(), i, None):\n", | |
| " return value\n", | |
| " raise IndexError(i)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 2", | |
| "language": "python", | |
| "name": "python2" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 2 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython2", | |
| "version": "2.7.10" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment