Question

I want to print an enumerated list in user specified columns.

import random

lst = random.sample(range(100), 30)
counter = 0

for pos, num in enumerate(sorted(lst)):
    if counter % 2 == 0:
        print '(%s) %s' %(pos, num),
        counter = counter + 1
    else:
        print '(%s) %s'.rjust(16) %(pos, num)
        counter = counter + 1

This gives:

(0) 3          (1) 6
(2) 7          (3) 10
(4) 11          (5) 13
(6) 17          (7) 18
(8) 20          (9) 25
(10) 45          (11) 46
(12) 48          (13) 51
(14) 58          (15) 59
(16) 60          (17) 63
(18) 68          (19) 69
(20) 77          (21) 81
(22) 83          (23) 84
(24) 87          (25) 89
(26) 93          (27) 94
(28) 97          (29) 98

How do I print a list where first 15 numbers are in the first column and second fifteen are in the second? I've seen some examples where range(start, stop, step) have been used to create sub-lists and resultant rows have been printed but I would have to lose enumeration and just print the numbers.

Was it helpful?

Solution

The "user specified" part of your question is tricky. How does the user specify what they want in each column? Can they specify more than two columns? etc.

However, if you know you have 30 items, and you know that you want the 1st 15 in the first column and the next 15 in the 2nd column I believe the following will do the trick:

for i in xrange(15):
    first = '(%s) %s' % (i, lst[i])
    padding = ' ' * (30 - len(first))
    second = '(%s) %s' % (i + 15, lst[i + 15])
    print '%s%s%s' % (first, padding, second)

OTHER TIPS

def print_table(seq, columns=2):
    table = ''
    col_height = len(seq) / columns
    for x in xrange(col_height):
        for col in xrange(columns):
            pos = (x * columns) + col
            num = seq[x + (col_height * col)]
            table += ('(%s) %s' % (pos, num)).ljust(16)
        table += '\n'
    print table

Results

>>> print_table(lst)
(0) 0           (1) 59          
(2) 3           (3) 61          
(4) 5           (5) 65          
(6) 7           (7) 75          
(8) 8           (9) 79          
(10) 17         (11) 81         
(12) 18         (13) 83         
(14) 22         (15) 84         
(16) 24         (17) 86         
(18) 43         (19) 88         
(20) 48         (21) 89         
(22) 49         (23) 92         
(24) 51         (25) 96         
(26) 52         (27) 97         
(28) 58         (29) 99         

>>> print_table(lst, 3)
(0) 0           (1) 48          (2) 81          
(3) 3           (4) 49          (5) 83          
(6) 5           (7) 51          (8) 84          
(9) 7           (10) 52         (11) 86         
(12) 8          (13) 58         (14) 88         
(15) 17         (16) 59         (17) 89         
(18) 18         (19) 61         (20) 92         
(21) 22         (22) 65         (23) 96         
(24) 24         (25) 75         (26) 97         
(27) 43         (28) 79         (29) 99         

>>> 

Still thinking of a way to specify between horizontal or verticle numbering.

Also, if the number of items in seq is not divisible by columns then the remainder won't get printed.

---edit---

Misunderstood the OP. There, just like Olivers.

def print_table(lst):
    table = ''
    half = len(lst) / 2
    for x1 in xrange(half):
        x2 = x1 + half
        col1 = '(%s) %s' % (x1, lst[x1])
        col2 = '(%s) %s\n' % (x2, lst[x2])
        table += col1.ljust(16) + col2
    print table
>>> lst
[87, 53, 82, 2, 76, 92, 98, 88, 74, 33, 15, 11, 58, 23, 10,
 65, 19, 83, 66, 38, 31, 21, 45, 85, 27, 84, 99, 55, 5, 26]
>>> hlen = len(lst)/2
>>> for i, t in enumerate(zip(lst, lst[hlen:])):
        print '({0:>2}) {2:>3}    ({1}) {3:>3}'.format(i, i+hlen, *t)

( 0)  87    (15)  65
( 1)  53    (16)  19
( 2)  82    (17)  83
( 3)   2    (18)  66
( 4)  76    (19)  38
( 5)  92    (20)  31
( 6)  98    (21)  21
( 7)  88    (22)  45
( 8)  74    (23)  85
( 9)  33    (24)  27
(10)  15    (25)  84
(11)  11    (26)  99
(12)  58    (27)  55
(13)  23    (28)   5
(14)  10    (29)  26
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top