Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在列中打印Python列表?_Python_List_Multiple Columns - Fatal编程技术网

如何在列中打印Python列表?

如何在列中打印Python列表?,python,list,multiple-columns,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, 3

我想在指定数量的对齐列中显示一个简单列表。例如,以下列表显示在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

如何做到这一点?

在元素之间添加一个选项卡,然后进行打印似乎对我有用:

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

在元素之间添加一个选项卡,然后进行打印似乎对我很有用:

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

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

from itertools import izip_longest

width = 14  
for lst in izip_longest(*[iter(a)]*width, fillvalue=''):
  print(('{:3} '*width).format(*lst))
请记住使用而不是
zip
,以便打印列表中的所有项目,从而解决缺少项目的问题



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

from itertools import izip_longest

width = 14  
for lst in izip_longest(*[iter(a)]*width, fillvalue=''):
  print(('{:3} '*width).format(*lst))
请记住使用而不是
zip
,以便打印列表中的所有项目,从而解决缺少项目的问题



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

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

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

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

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

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

而我另一个不含itertools的简单解决方案:

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

当i时,下面计算出填充每个条目所需的最大位数。因此,在这种情况下需要
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
与填充值一起用于填充任何剩余条目。这将为您提供以下输出:

1234567891011214
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
,它将自动调整如下:

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

下面计算出填充每个条目所需的最大位数。因此,在这种情况下需要
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
与填充值一起用于填充任何剩余条目。这将为您提供以下输出:

1234567891011214
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
,它将自动调整如下:

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

这称为漂亮的打印。如果python没有一个函数可以为您完成这项工作,那么您需要在较小的数字上填充空格,以便所有内容都对齐。这称为“漂亮打印”。如果python没有一个函数可以为您实现这一点,那么您需要用空格填充较小的数字,以便所有内容都对齐。打印后列表将变为空。打印后列表将变为空。