Created
July 26, 2013 00:47
-
-
Save professorplumb/6085156 to your computer and use it in GitHub Desktop.
Several Python sequence-related functions I find myself using over and over again and tire of reimplementing. :) These probably all originally came from StackOverflow answers of one sort or another.
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
from itertools import tee, izip, izip_longest | |
def pairwise_overlapping(iterable, n=2): | |
""" | |
Given a sequence, returns a list of pairs/tuples of length n, each comprised of sequential values | |
from the sequence. Each tuple starts with the subsequent value from the original sequence. | |
>>> s = 1, 2, 3, 4, 5 | |
>>> list(pairwise_overlapping(s)) | |
[(1, 2), (2, 3), (3, 4), (4, 5)] | |
>>> list(pairwise_overlapping(s, 3)) | |
[(1, 2, 3), (2, 3, 4), (3, 4, 5)] | |
""" | |
seqs = tee(iterable, n) | |
for advance_count, seq in enumerate(seqs, start=0): | |
for _ in range(advance_count): | |
next(seq, None) | |
return izip(*seqs) | |
def pairwise_exclusive(iterable): | |
""" | |
Given a sequence, returns a list of pairs, each comprised of sequential values from the sequence. | |
If a given pair consists of items n and n+1, the next will start with item n + 2. | |
The final item in a sequence with an odd length will be discarded. (To preserve it as (n, None) | |
use izip_longest instead of izip). | |
>>> s = 1, 2, 3, 4, 5 | |
>>> list(pairwise_exclusive(s)) | |
[(1, 2), (3, 4)] | |
>>> s = 1, 2, 3, 4, 5, 6 | |
>>> list(pairwise_exclusive(s)) | |
[(1, 2), (3, 4), (5, 6)] | |
""" | |
a = iter(iterable) | |
return izip(a, a) | |
def chunks(iterable, chunk_size, fillvalue=None): | |
""" | |
The same as pairwise_exclusive but will divide sequences into "chunks" of any size. Preserves all | |
values in original sequence, padding final tuples with fillvalue if necessary. | |
chunks(iterable, 2) is exactly equivalent to pairwise_exclusive using izip_longest instead of izip. | |
>>> s = 1, 2, 3, 4, 5 | |
>>> list(chunks(s, 2)) | |
[(1, 2), (3, 4), (5, None)] | |
>>> list(chunks(s, 3)) | |
[(1, 2, 3), (4, 5, None)] | |
""" | |
return izip_longest(*[iter(iterable)] * chunk_size, fillvalue=fillvalue) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment