compcrk
compcrk

Reputation: 437

how to print lists within a tuple in straight line

I have a python program in which I have populated an tuple with a 7 lists, each of 5 integers. I want them to be printed in straight lines like this:

ROW1   R0W2   ROW3   ROW4   ROW 5
 3      5      7      6       5
 5      4      3      7       8

and so on...

I have coded the following, buts its printing in straight lines:

numbers= [[3,4,5,6,7],[4,5,7,8,9],[6,7,9,5,3],[3,23,56,67,4],
         [54,67,4,3,2],[4,5,6,7,7],[8,8,8,8,8]]
variable = 1           
for w1 in numbers:
    print 'Row:', variable,
    for w2 in w1:
        print w2,
variable += 1

Upvotes: 2

Views: 511

Answers (2)

SirParselot
SirParselot

Reputation: 2700

Well first off you have a list of lists, not a tuple of lists, and pandas can do this for you very easily. You can use pandas to turn it into a dataframe and then print the dataframe.

import pandas as pd

numbers= [[3,4,5,6,7],[4,5,7,8,9],[6,7,9,5,3],[3,23,56,67,4],
         [54,67,4,3,2],[4,5,6,7,7],[8,8,8,8,8]]

d = pd.DataFrame(numbers, columns=['Row1','Row2','Row3','Row4','Row5'])
print d

   Row1  Row2  Row3  Row4  Row5
0     3     4     5     6     7
1     4     5     7     8     9
2     6     7     9     5     3
3     3    23    56    67     4
4    54    67     4     3     2
5     4     5     6     7     7
6     8     8     8     8     8

If you want the lists vertical you will need to transpose your dataframe and then name the columns like so

import pandas as pd

numbers= [[3,4,5,6,7],[4,5,7,8,9],[6,7,9,5,3],[3,23,56,67,4],
         [54,67,4,3,2],[4,5,6,7,7],[8,8,8,8,8]]

d = pd.DataFrame(numbers).T
d.columns=['Row1','Row2','Row3','Row4','Row5','Row6','Row7']
print d

   Row1  Row2  Row3  Row4  Row5  Row6  Row7
0     3     4     6     3    54     4     8
1     4     5     7    23    67     5     8
2     5     7     9    56     4     6     8
3     6     8     5    67     3     7     8
4     7     9     3     4     2     7     8

Upvotes: 0

Padraic Cunningham
Padraic Cunningham

Reputation: 180401

You can use str.format mini-language, you will need to get the number with the greatest number of digits and use that justify or center to get it to work for any data:

numbers = [[3, 4, 5, 6, 7], [4, 5, 7, 8, 9], [6, 7, 9, 5, 3], [3, 23, 56, 67, 4],
           [54, 67, 4, 3, 2], [4, 5, 6, 7, 7], [8, 8, 8, 8, 8]]

print(" ".join(["Row{:<2}".format(i) for i in range(1, len(numbers[0])+1)]))
for w1 in numbers:
    print("".join(["{:^6}".format(i) for i in w1]))

Output centered ^:

Row1  Row2  Row3  Row4  Row5 
  3     4     5     6     7   
  4     5     7     8     9   
  6     7     9     5     3   
  3     23    56    67    4   
  54    67    4     3     2   
  4     5     6     7     7   
  8     8     8     8     8   

And left justified:

print(" ".join(["Row{:<2}".format(i) for i in range(1, len(numbers[0])+1)]))
for w1 in numbers:
    print("".join(["{:<6}".format(i) for i in w1]))

Output:

Row1  Row2  Row3  Row4  Row5 
3     4     5     6     7     
4     5     7     8     9     
6     7     9     5     3     
3     23    56    67    4     
54    67    4     3     2     
4     5     6     7     7     
8     8     8     8     8     

If you want each sublist to be a column you can transpose with zip:

print("".join(["Row{:<3}".format(i) for i in range(1, len(numbers)+1)]))
for w1 in zip(*numbers):
    print("".join(["{:<6}".format(i) for i in w1]))

Output:

Row1  Row2  Row3  Row4  Row5  Row6  Row7  
3     4     6     3     54    4     8     
4     5     7     23    67    5     8     
5     7     9     56    4     6     8     
6     8     5     67    3     7     8     
7     9     3     4     2     7     8   

To work for any data we need to find what will be the longest string which we can do with max and use that as the amount to justify by:

numbers = [[3, 4, 5, 6, 7], [4, 5, 3333232327, 8, 9], [6, 7, 9, 5, 3], [3, 23, 56, 67, 4],
           [54, 67, 4, 3, 2], [4, 5, 6, 7, 3237], [8, 8, 8, 8, 8] ]
mx = max(len(str(i)) for sub in numbers for i in sub) + 1

print("".join(["Row{:<{mx}}".format(i,mx=mx-3) for i in range(1, len(numbers)+1)]))
for w1 in zip(*numbers):
    print("".join(["{:<{mx}}".format(i, mx=mx) for i in w1]))

Output:

Row1       Row2       Row3       Row4       Row5       Row6       Row7       
3          4          6          3          54         4          8          
4          5          7          23         67         5          8          
5          3333232327 9          56         4          6          8          
6          8          5          67         3          7          8          
7          9          3          4          2          3237       8    

Upvotes: 3

Related Questions