在PYTHON

时间:2017-01-22 07:49:40

标签: python

拜托,我需要帮助。我怎样才能做到这一点? 对齐的列和行,并向左调整。

可能是,这些是变量:

a1=5548867423267
b1=4
c1=11346778
d1=344
e1=67
f1=9745
g1=9745
h1=9745
a2=55467423267
b2=7
c2=1134476778
d2=344447774
e2=647
f2=9744775
g2=97474545
h2=974776545
a3=5548867423267
b3=4
c3=11346778
d3=344
e3=67
f3=9745
g3=9745
h3=9745
a4=5548867423267
b4=4
c4=11346778
d4=344
e4=67
f4=9745
g4=9745
h4=9745

这些是包含文本和变量的列表:

L1 = [" ","A","B","C","D","E","F","G","H"]
L2 = ["CLASS1",a1,b1,c1,d1,e1,f1,g1,h1]
L3 = ["CLASS2",a2,b2,c2,d2,e2,f2,g2,h2]
L4 = ["CLASS3",a3,b3,c3,d3,e3,f3,g3,h3]
L5 = ["CLASS4",a4,b4,c4,d4,e4,f4,g4,h4]

我希望结果像这样:

         A               B          C              D   E  F    G    H
CLASS1   5548867423267   4          11346778       344 67 9745 9745 974
CLASS2   .......         .          ........       ... .. 9745 9745 974
CLASS3   .............   .          ........       ... .. 9745 9745 974
CLASS4   ....            .          ........       ... .. 9745 9745 974

1 个答案:

答案 0 :(得分:0)

使用str.format()函数的解决方案:

for cl, a,b,c,d,e,f,g,h in [L1,L2,L3,L4,L5]:
    print('{:<10} {:<15} {:<10} {:<15} {:<10} {:<10} {:<10} {:<10} {:<10}'.format(cl,a,b,c,d,e,f,g,h))

输出:

           A               B          C               D          E          F          G          H         
CLASS1     5548867423267   4          11346778        344        67         9745       9745       9745      
CLASS2     55467423267     7          1134476778      344447774  647        9744775    97474545   974776545 
CLASS3     5548867423267   4          11346778        344        67         9745       9745       9745      
CLASS4     5548867423267   4          11346778        344        67         9745       9745       9745