Skip to content

Instantly share code, notes, and snippets.

@mattdeboard
Created May 10, 2012 22:53
Show Gist options
  • Select an option

  • Save mattdeboard/2656434 to your computer and use it in GitHub Desktop.

Select an option

Save mattdeboard/2656434 to your computer and use it in GitHub Desktop.
def take(n, seq):
"Return first n items of the seq as a list"
return list(islice(seq, n))
def drop(n, seq):
"Return seq with the first n items removed."
return list(islice(seq, n, None, 1))
def split_by(n, seq):
return [take(n, seq), drop(n, seq)]
def slices(seq, start=0, end=None, step=2):
fun = split_by
ls = len(seq)
if ls < step and seq:
yield (0, ls)
else:
if ls % step:
rng = xrange(ls/step+1)
else:
rng = xrange(ls/step)
for i in rng:
segment = fun(step, seq[start:])
start += step
yield segment[0][0], segment[0][-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment