【问题标题】:How to use % to print a big list of number? [closed]如何使用 % 打印一大串数字? [关闭]
【发布时间】:2020-12-22 01:12:49
【问题描述】:

我有这种格式,我需要遵守其他格式来打印数字列表(列表有超过 200 个数字):

my_format = '%6d%6d%6d%6d%6d%6d%6d%6d%6d%6d%6d%6d%6d%6d%6d\n'
my_list = [1,2,3,4,5,6,199, 57, .........]

基本上就是 %6d 重复了 15 次。

当你使用 % 格式化字符串时,我知道你需要这样做:

with open('my_out.txt', 'w') as fout:
    fout.write(my_format % (num1, num2, num3, num4, num5, ....., num15)

在我的情况下,我需要将 my_list 分成 15 个部分,在最后一个部分中,我可能没有 15 个数字;因此,我需要以某种方式忽略 my_list 中没有项目的变量 num 因为我已经遍历了所有数字。另外,考虑到我需要创建 15 个变量(从 num1 到 num15),所以我可以使用:

my_format % (num1, num2, num3, num4, num5, ....., num15)

我的文件应该是这样的:

0 1 2 3 4 5 6 7 8 9 1 1 2 1 1

1 4 5 8 9 7 5 8 9 7 6 5 4 3 0

3 4 6 7 1 8

有没有一种聪明有效的方法可以解决我的问题?

【问题讨论】:

    标签: python python-3.x string


    【解决方案1】:

    只需使用%6d 格式化单个项目,然后将它们加入空字符串并在末尾添加换行符:

    nums = list(range(15))
    formatted = "".join("%6d" % n for n in nums) + "\n"
    
    print(formatted)
    #      0     1     2     3     4     5     6     7     8     9    10    11    12    13    14
    

    【讨论】:

    • 你能帮我举个例子,你使用 nums = list(range(20))。如何在第一行只取 15 个,然后在最后一行取剩下的 5 个
    • 如果您只需要前 15 个而不是每 15 个,那么只需切片:formatted = "".join("%6d" % n for n in nums[:15]) + "\n"
    • 我使用了您提供的链接。我能够将 my_list 分成 15 个项目的块。当我运行您的代码时:formatted = "".join("%6d" % n for n in nums) + "\n",我收到错误:TypeError: %d format: a number is required, not list跨度>
    • 在对列表进行分块后,您必须将格式循环到每个元素上。
    【解决方案2】:

    这不是一种聪明(甚至是 Pythonic)的方式,但它可以完成工作:

    with open('my_out.txt', 'w') as fout:
        while len(my_list) > 0:
            str = ''
            for loop in range(0, 15):
                try:
                    str += '%6d' % my_list.pop(0)
                except:
                    break
            str += '\n'
            fout.write(str)
    

    【讨论】:

      【解决方案3】:
      def chunks(lst, n):
          """Yield successive n-sized chunks from lst."""
          for i in range(0, len(lst), n):
              yield lst[i:i + n]
      
      list_chunks = list(chunks(my_list, 15))
      
      all_item = []
      for element in x:
          formatted = "".join("%6d" % n for n in element) + "\n"
          all_item.append(formatted)
      

      您可以将其作为列表或直接打印到文件中。

      解决方案由@Aplet123 提供。我只是把所有东西放在一起

      【讨论】:

        【解决方案4】:
        def chunks(lst, n):
            for i in range(0, len(lst), n):
                yield lst[i:i + n]
        
        my_list = [f'{x:6d}' for x in list(range(300))]
        
        for lst in list(chunks(my_list, 15)):
            print(' '.join(lst))
        
             0      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    121    122    123    124    125    126    127    128    129    130    131    132    133    134
           135    136    137    138    139    140    141    142    143    144    145    146    147    148    149
           150    151    152    153    154    155    156    157    158    159    160    161    162    163    164
           165    166    167    168    169    170    171    172    173    174    175    176    177    178    179
           180    181    182    183    184    185    186    187    188    189    190    191    192    193    194
           195    196    197    198    199    200    201    202    203    204    205    206    207    208    209
           210    211    212    213    214    215    216    217    218    219    220    221    222    223    224
           225    226    227    228    229    230    231    232    233    234    235    236    237    238    239
           240    241    242    243    244    245    246    247    248    249    250    251    252    253    254
           255    256    257    258    259    260    261    262    263    264    265    266    267    268    269
           270    271    272    273    274    275    276    277    278    279    280    281    282    283    284
           285    286    287    288    289    290    291    292    293    294    295    296    297    298    299
        

        【讨论】:

          猜你喜欢
          • 2021-02-20
          • 2011-09-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-20
          相关资源
          最近更新 更多