Question

I currently have a list of strings x:

[ '15.0000291159500048', '15.0000203137357904', '14.9999702802477710', '5.0000017127321428', '-5.0000029074634842', '0.0000000599304253', '0.0000001437028190', '-0.0000000724688421', '-0.0000000510019702' ]

Which I would like to print to a text file as a square by adding a newline after n elements where n = sqrt(len(x)) yielding a text file with output:

15.0000291159500048 15.0000203137357904 14.9999702802477710
5.0000017127321428 -5.0000029074634842 0.0000000599304253
0.0000001437028190 -0.0000000724688421 -0.0000000510019702

Anyone know of a clever way to accomplish this? Must I use a counter with modulus or can python do this innately more effeciently?

Was it helpful?

Solution

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)

OTHER TIPS

You can use list comprehension and str.join:

In [26]: lis = [ '15.0000291159500048', '15.0000203137357904', '14.9999702802477
710', '5.0000017127321428', '-5.0000029074634842', '0.0000000599304253', '0.0000
001437028190', '-0.0000000724688421', '-0.0000000510019702' ]

In [27]: n = 3

In [28]: print "\n".join([' '.join(lis[i:i+n]) for i in xrange(0, len(lis), n)])

15.0000291159500048 15.0000203137357904 14.9999702802477710
5.0000017127321428 -5.0000029074634842 0.0000000599304253
0.0000001437028190 -0.0000000724688421 -0.0000000510019702
l = [ '15.0000291159500048', '15.0000203137357904', '14.9999702802477710', '5.0000017127321428', '-5.0000029074634842', '0.0000000599304253', '0.0000001437028190', '-0.0000000724688421', '-0.0000000510019702' ]  
stepSize = int(sqrt(len(l)))
print '\n'.join([' '.join(l[i:i+stepSize]) for i in range(0, len(l), stepSize)])

Based on the answer for chunking python lists here. We just choose a chunk size equal to the square root the length of the list. You could change this to round instead of truncate the square root.

The comprehension will retrieve sub-lists with the correct length, joining them together with a space. The result will be a list of strings showing your result separated by a space. We then join this together with a newline

So this might be a bit nuts, but here's another approach.

x = [ '15.0000291159500048', '15.0000203137357904', '14.9999702802477710', '5.0000017127321428', '-5.0000029074634842', '0.0000000599304253', '0.0000001437028190', '-0.0000000724688421', '-0.0000000510019702' ] s = math.sqrt(len(x)) matrix = [[x[a+b] for z in xrange(int(s)) ] for y in range(0, len(x), int(s)) ] print "\n".join( map(lambda innerRow: " ".join( map(lambda item: str(item), innerRow)), matrix))

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top