Created
November 16, 2013 09:39
-
-
Save shawwn/7498162 to your computer and use it in GitHub Desktop.
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
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: | |
sys.stdout.write(line) | |
process(sys.argv[1:]) | |
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
C:\Users\Shawn>python test3.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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment