Skip to content

Instantly share code, notes, and snippets.

@buchanae
Last active December 16, 2015 03:09
Show Gist options
  • Save buchanae/5367597 to your computer and use it in GitHub Desktop.
Save buchanae/5367597 to your computer and use it in GitHub Desktop.
from collections import defaultdict
def splitter(lines, keyfunc, valuefunc):
d = defaultdict(list)
for line in lines:
cols = line.split()
key = keyfunc(cols)
value = valuefunc(cols)
d[key].append(value)
return d
TEST_TEXT = '''
one 1 bar
two 2 baz
foo 1 bat
'''.strip()
if __name__ == '__main__':
lines = TEST_TEXT.split('\n')
keyfunc = lambda cols: cols[1]
valuefunc = lambda cols: (cols[0], cols[2])
expected = {
'1': [('one', 'bar'), ('foo', 'bat')],
'2': [('two', 'baz')],
}
d = splitter(lines, keyfunc, valuefunc)
assert d == expected
# works with files too
from StringIO import StringIO
fake_fh = StringIO(TEST_TEXT)
d = splitter(fake_fh, keyfunc, valuefunc)
assert d == expected
# write splitter dictionary
for key, values in d.items():
with open(key, 'w') as out:
for value in values:
out.write(' '.join(value) + '\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment