Printing Columns
Mike C. Fletcher
mcfletch at rogers.com
Tue Apr 15 14:17:02 EDT 2003
Straightforward approach (will be inefficient if you really do approach
infinity though ;) )...
>>> def indices( total, columns = 3 ):
... rows, remainder = divmod( total, columns )
... if remainder:
... rows += 1
... return [
... range( row, total, rows )
... for row in xrange(rows)
... ]
...
>>> def columnMajor( items, columns = 3, format = '%8d', joint="|"):
... for row in indices(len(items),columns):
... print joint.join([format%item for item in row])
...
>>> columnMajor( range(23),4)
0| 6| 12| 18
1| 7| 13| 19
2| 8| 14| 20
3| 9| 15| 21
4| 10| 16| 22
5| 11| 17
>>>
HTH,
Mike
Lindstrom Greg - glinds wrote:
...
>Given a list of length N with 0<=N<=infinity and a number of columns, C (C>0
>and may be less than N), how do we print out the elements of the list in the
>above (column major?) form?
>
>
...
_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
More information about the Python-list
mailing list