如何以直线打印元组内的列表

时间:2015-11-17 20:28:35

标签: python list loops tuples

我有一个python程序,我在其中填充了一个带有7个列表的元组,每个列表有5个整数。我想让它们像这样直线印刷:

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

依旧......

我编写了以下内容,但是它是以直线打印的:

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

2 个答案:

答案 0 :(得分:3)

您可以使用str.format mini-language,您需要获取具有最大位数的数字,并使用该数字或中心来使其适用于任何数据:

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]))

^为中心的输出:

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   

左派合理:

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]))

输出:

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     

如果您希望每个子列表都是一列,您可以使用zip:transpose使用<:p>

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]))

输出:

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   

为了处理任何数据,我们需要找到最长的字符串,我们可以用max做什么,并使用它作为证明的数量:

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]))

输出:

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    

答案 1 :(得分:0)

首先,你有一个列表列表,而不是列表元组,大熊猫可以很容易地为你做到这一点。您可以使用pandas将其转换为数据帧,然后打印数据帧。

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

如果您希望列表垂直,则需要转置数据框,然后将列命名为

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