Skip to content

Instantly share code, notes, and snippets.

@shawwn
Created November 16, 2013 09:37
Show Gist options
  • Save shawwn/7498152 to your computer and use it in GitHub Desktop.
Save shawwn/7498152 to your computer and use it in GitHub Desktop.
import sys
import pdb
# iterate over 'blocksize' elems of a list at once.
def blocks(xs, blocksize):
for i in xrange(0, len(xs), blocksize):
yield xs[i:i+blocksize]
# partitions a list into groups of length blocksize. E.g.:
def blocklist(xs, blocksize):
return list(blocks(xs, blocksize))
assert(blocklist([1, 2, 3, 4, 5, 6], 2) == [[1, 2], [3, 4], [5, 6]])
assert(blocklist([1, 2, 3, 4, 5, 6], 3) == [[1, 2, 3], [4, 5, 6]])
assert(blocklist([1, 2, 3, 4, 5, 6], 4) == [[1, 2, 3, 4], [5, 6]])
def process(files):
fileslines = []
for filename in files:
with open(filename, 'rb') as f:
fileslines.append(f.readlines())
filesgroups = [blocklist(x, 5) for x in fileslines]
for groups in zip(*filesgroups):
for group in groups:
for line in group:
print line
process(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment