Skip to content

Instantly share code, notes, and snippets.

@shawwn
Created November 16, 2013 09:41
Show Gist options
  • Save shawwn/7498174 to your computer and use it in GitHub Desktop.
Save shawwn/7498174 to your computer and use it in GitHub Desktop.
C:\Users\Shawn>python process.py test.txt test2.txt test3.txt
foo1
foo2
foo3
foo4
foo5
bar1
bar2
bar3
bar4
bar5
baz1
baz2
baz3
baz4
baz5
foo6
foo7
foo8
foo9
foo10
bar6
bar7
bar8
bar9
bar10
baz6
baz7
baz8
baz9
baz10
import sys
# 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:
sys.stdout.write(line)
process(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment