Using the grouper
recipe from the itertools documentation:
>>> data = [ '15.0000291159500048', '15.0000203137357904', '14.9999702802477710', '5.0000017127321428', '-5.0000029074634842', '0.0000000599304253', '0.0000001437028190', '-0.0000000724688421', '-0.0000000510019702' ]
>>> n = int(math.sqrt(len(data)))
>>> print '\n'.join(' '.join(row) for row in grouper(data, n, ''))
15.0000291159500048 15.0000203137357904 14.9999702802477710
5.0000017127321428 -5.0000029074634842 0.0000000599304253
0.0000001437028190 -0.0000000724688421 -0.0000000510019702
Here is what grouper()
looks like:
from itertools import izip_longest
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)