Skip to content

Instantly share code, notes, and snippets.

@c-garcia
Created September 18, 2015 07:38
Show Gist options
  • Save c-garcia/b4311c7c3b1804aeaecd to your computer and use it in GitHub Desktop.
Save c-garcia/b4311c7c3b1804aeaecd to your computer and use it in GitHub Desktop.
Python equivalent to clojure partition (simple case)
def partition(seq, num):
"Partititons a seq en disjoint seqs of up to num elems"
return [seq[start:start+count] \
for (start, count) in \
[(num * x,y) for (x,y) in \
zip(range(len(seq)/num), [num]*(num*(len(seq)/num)))] +
[(num * (len(seq)/num), len(seq) % num)]
if count >0]
@c-garcia
Copy link
Author

Having seen the generator https://news.ycombinator.com/item?id=668544 , mine is just an intellectual exercise :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment