DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Santiago has posted 1 posts at DZone. View Full User Profile

Split Array Or String Into Smaller Arrays

06.13.2008
| 18794 views |
  • submit to reddit
        Splits an array or string into smaller arrays of the given size. Inspired on the ruby one ;).

import math
# v = value to split, l = size of each chunk
f = lambda v, l: [v[i*l:(i+1)*l] for i in range(int(math.ceil(len(v)/float(l))))]

Example

>>> f('000000111111222222333333444444567', 6)
['000000', '111111', '222222', '333333', '444444', '567']

>>> f(tuple('000000111111222222333333444444567'), 6)
[('0', '0', '0', '0', '0', '0'), ('1', '1', '1', '1', '1', '1'), ('2', '2', '2', '2', '2', '2'), ('3', '3', '3', '3', '3', '3'), ('4', '4', '4', '4', '4', '4'), ('5', '6', '7')]

Long live python :). Of course, a lisp/haskell implementation probably blows this snippet out.    

Comments

Snippets Manager replied on Tue, 2007/06/05 - 11:43am

small error in last implementation: def group(s, n): return [s[i:i+n:n] for i in xrange(0, len(s), n)] gives me: >>> group('aaabbbccc', 3) ['a', 'b', 'c'] but this def group(s, n): return [s[i:i+n] for i in xrange(0, len(s), n)] works for me: >>> group('aaabbbccc', 3) ['aaa', 'bbb', 'ccc']

Snippets Manager replied on Mon, 2009/11/30 - 7:07pm

A much simpler implementation: def group(s, n): return [s[i:i+n:n] for i in xrange(0, len(s), n)] Returns every n elements of s as a contiguous list. >>> group([1, 2, 3, 4, 5, 6], 2) [[1, 2], [3, 4], [5, 6]] >>> group('aaabbbccc', 3) ['aaa', 'bbb', 'ccc'] ^D

Paddy 3118 replied on Sat, 2008/06/14 - 1:34am

Hmm, I could only come up with the following: >>> from itertools import groupby >>> s = '000000111111222222333333444444567' >>> # split on runs of the the same character >>> [''.join(x[1]) for x in groupby(s, lambda x: x)] ['000000', '111111', '222222', '333333', '444444', '5', '6', '7'] >>> # Split on every n'th character (ugly) >>> n = 6 >>> [''.join(z[1] for z in y[1]) for y in groupby([[i/n, char] for i,char in enumerate(s)], lambda x: x[0])] ['000000', '111111', '222222', '333333', '444444', '567'] >>> About the only thing that my example has going for it is that it does not rely on imprecise floating point calcs. But other than that, its 'orrible. - Paddy.