如何在列中打印Python列表?

时间:2017-06-28 15:01:09

标签: python list multiple-columns

我想在指定数量的对齐列中显示一个简单列表。例如,以下是14列中显示的列表,列之间的空格数在3和1之间变化:

a = [
         1,    2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,
        15,   16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,
        29,   30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,
        43,   44,  45,  46,  47,  48,  49,  50,  51,  52,  53,  54,  55,  56,
        57,   58,  59,  60,  61,  62,  63,  64,  65,  66,  67,  68,  69,  70,
        71,   72,  73,  74,  75,  76,  77,  78,  79,  80,  81,  82,  83,  84,
        85,   86,  87,  88,  89,  90,  91,  92,  93,  94,  95,  96,  97,  98,
        99,  100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
        113, 114, 115, 116, 117, 118, 119, 120
    ]

我最初的尝试如下:

for a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14 in zip(*[iter(a)] * 14):
    print(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14)

但是,这会导致列错位和缺少列表元素:

1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 16 17 18 19 20 21 22 23 24 25 26 27 28
29 30 31 32 33 34 35 36 37 38 39 40 41 42
43 44 45 46 47 48 49 50 51 52 53 54 55 56
57 58 59 60 61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80 81 82 83 84
85 86 87 88 89 90 91 92 93 94 95 96 97 98
99 100 101 102 103 104 105 106 107 108 109 110 111 112

怎么可以这样做?

5 个答案:

答案 0 :(得分:3)

在元素之间添加标签然后打印似乎对我有用:

for i in range(0, len(a)-1, 14):
    print('\t'.join(map(str, a[i:i+14])))

1   2   3   4   5   6   7   8   9   10  11  12  13  14
15  16  17  18  19  20  21  22  23  24  25  26  27  28
29  30  31  32  33  34  35  36  37  38  39  40  41  42
43  44  45  46  47  48  49  50  51  52  53  54  55  56
57  58  59  60  61  62  63  64  65  66  67  68  69  70
71  72  73  74  75  76  77  78  79  80  81  82  83  84
85  86  87  88  89  90  91  92  93  94  95  96  97  98
99  100 101 102 103 104 105 106 107 108 109 110 111 112
113 114 115 116 117 118 119 120

答案 1 :(得分:3)

您可以使用一些字符串格式:

from itertools import izip_longest

width = 14  
for lst in izip_longest(*[iter(a)]*width, fillvalue=''):
  print(('{:3} '*width).format(*lst))

请记住使用itertools.izip_longest代替zip,以便打印列表中的所有项目,解决缺失的项目问题。

  1   2   3   4   5   6   7   8   9  10  11  12  13  14 
 15  16  17  18  19  20  21  22  23  24  25  26  27  28 
 29  30  31  32  33  34  35  36  37  38  39  40  41  42 
 43  44  45  46  47  48  49  50  51  52  53  54  55  56 
 57  58  59  60  61  62  63  64  65  66  67  68  69  70 
 71  72  73  74  75  76  77  78  79  80  81  82  83  84 
 85  86  87  88  89  90  91  92  93  94  95  96  97  98 
 99 100 101 102 103 104 105 106 107 108 109 110 111 112 
113 114 115 116 117 118 119 120 

答案 2 :(得分:3)

这不是一个漂亮的解决方案,但您可以根据整数的字符串长度添加空格:

a = [
         1,    2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,
        15,   16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,
        29,   30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,
        43,   44,  45,  46,  47,  48,  49,  50,  51,  52,  53,  54,  55,  56,
        57,   58,  59,  60,  61,  62,  63,  64,  65,  66,  67,  68,  69,  70,
        71,   72,  73,  74,  75,  76,  77,  78,  79,  80,  81,  82,  83,  84,
        85,   86,  87,  88,  89,  90,  91,  92,  93,  94,  95,  96,  97,  98,
        99,  100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
        113, 114, 115, 116, 117, 118, 119, 120
    ]


maxLen = len(str(max(a)))
a = [str(A)+" "*(maxLen-len(str(A))) for A in a]

for a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14 in zip(*[iter(a)] * 14):
    print(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14)

输出:

1   2   3   4   5   6   7   8   9   10  11  12  13  14 
15  16  17  18  19  20  21  22  23  24  25  26  27  28 
29  30  31  32  33  34  35  36  37  38  39  40  41  42 
43  44  45  46  47  48  49  50  51  52  53  54  55  56 
57  58  59  60  61  62  63  64  65  66  67  68  69  70 
71  72  73  74  75  76  77  78  79  80  81  82  83  84 
85  86  87  88  89  90  91  92  93  94  95  96  97  98 
99  100 101 102 103 104 105 106 107 108 109 110 111 112

答案 3 :(得分:1)

没有itertools的另一个简单解决方案:

i=0
while i<len(a):
  for j in range(i,min(i+14,len(a))):
    print '%4d' % (a[j]),
  print
  i+=14

答案 4 :(得分:1)

以下计算出填充每个条目所需的最大位数。因此,在这种情况下,3是必需的。如果1000已添加到列表中,则会4

from itertools import izip_longest    
import math        

a = [
     1,    2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,
    15,   16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,
    29,   30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,
    43,   44,  45,  46,  47,  48,  49,  50,  51,  52,  53,  54,  55,  56,
    57,   58,  59,  60,  61,  62,  63,  64,  65,  66,  67,  68,  69,  70,
    71,   72,  73,  74,  75,  76,  77,  78,  79,  80,  81,  82,  83,  84,
    85,   86,  87,  88,  89,  90,  91,  92,  93,  94,  95,  96,  97,  98,
    99,  100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
    113, 114, 115, 116, 117, 118, 119, 120
    ]

pad = '{{:{}}}'.format(int(math.ceil(math.log(max(a)+1) / math.log(10))))

for row in izip_longest(*[iter(a)] * 14, fillvalue=''):
    print ' '.join(pad.format(v) for v in row)

izip_longest与填充值一起使用以填充任何剩余的条目。这会给你以下输出:

  1   2   3   4   5   6   7   8   9  10  11  12  13  14
 15  16  17  18  19  20  21  22  23  24  25  26  27  28
 29  30  31  32  33  34  35  36  37  38  39  40  41  42
 43  44  45  46  47  48  49  50  51  52  53  54  55  56
 57  58  59  60  61  62  63  64  65  66  67  68  69  70
 71  72  73  74  75  76  77  78  79  80  81  82  83  84
 85  86  87  88  89  90  91  92  93  94  95  96  97  98
 99 100 101 102 103 104 105 106 107 108 109 110 111 112
113 114 115 116 117 118 119 120

如果您的数字列表仅上升50,则会自动调整如下:

 1  2  3  4  5  6  7  8  9 10 11 12 13 14
15 16 17 18 19 20 21 22 23 24 25 26 27 28
29 30 31 32 33 34 35 36 37 38 39 40 41 42
43 44 45 46 47 48 49 50