Python3 Display certain number of output

Question:

I have a list a numbers, I want to display 10 per line, and they have to format really nice

start: 1
End: 1000
   2    3    5    7   11   13   17   19   23   29 
  31   37   41   43   47   53   59   61   67   71 
  73   79   83   89   97  101  103  107  109  113 
 127  131  137  139  149  151  157  163  167  173 
 179  181  191  193  197  199  211  223  227  229 
 233  239  241  251  257  263  269  271  277  281 
 283  293  307  311  313  317  331  337  347  349 
 353  359  367  373  379  383  389  397  401  409 
 419  421  431  433  439  443  449  457  461  463 
 467  479  487  491  499  503  509  521  523  541 
 547  557  563  569  571  577  587  593  599  601 
 607  613  617  619  631  641  643  647  653  659 
 661  673  677  683  691  701  709  719  727  733 
 739  743  751  757  761  769  773  787  797  809 
 811  821  823  827  829  839  853  857  859  863 
 877  881  883  887  907  911  919  929  937  941 
 947  953  967  971  977  983  991  997 

The format has to be like this, the “2” in the beginning has to be at the top of “1” in “31” and so on.

So I tried some solution

gen = ('%-5sn' % x if i%10==0 else '%-5s' %x for i,x in enumerate(prime_list,1))

This will give me something like this

1    2    3    5    7    11   13   17   19   23
29   31   37   41   43   47   53   59   61   67
71   73   79   83   89   97   101  103  107  109
113  127  131  137  139  149  151  157  163  167
173  179  181  191  193  197  199  211  223  227
229  233  239  241  251  257  263  269  271  277
281  283  293  307  311  313  317  331  337  347
349  353  359  367  373  379  383  389  397  401
409  419  421  431  433  439  443  449  457  461
463  467  479  487  491  499  503  509  521  523
541  547  557  563  569  571  577  587  593  599
601  607  613  617  619  631  641  643  647  653
659  661  673  677  683  691  701  709  719  727
733  739  743  751  757  761  769  773  787  797
809  811  821  823  827  829  839  853  857  859
863  877  881  883  887  907  911  919  929  937
941  947  953  967  971  977  983  991  997

Exactly the opposite I want. Anyone have any idea?

Thank you!

Asked By: jifferent

||

Answers:

Try gen = ('%+5sn' % x if i%10==0 else '%+5s' %x for i,x in enumerate(prime_list,1)), instead of gen = ('%-5sn' % x if i%10==0 else '%-5s' %x for i,x in enumerate(prime_list,1))

>>> nums = [2,32,34,3,54,5,436,45,6,45,674,57,56,87,567,2]
>>> gen = ('%+5sn' % x if i%10==0 else '%+5s' %x for i,x in enumerate(nums,1))
>>> for i in nums:
...     print(next(gen))
...
    2
   32
   34
    3
   54
    5
  436
   45
    6
   45
  674
   57
   56
   87
  567
    2
Answered By: khajvah

1 is not a prime.
Therefore, prime_list should not contain 1. Ideally, prime_list should be generated without 1 in it. If you have to correct the mistake after-the-fact, then use

prime_list = prime_list[1:]

Once that is corrected, your code works fine:

print(''.join('%-5sn' % x if i%10==0 else '%-5s' %x for i,x in enumerate(prime_list,1)))

yields

2    3    5    7    11   13   17   19   23   29   
31   37   41   43   47   53   59   61   67   71   
...
947  953  967  971  977  983  991  997  

Alternatively, you could split the problem into smaller parts:

  1. format each int as a left-justified string of length 5
  2. use the grouper recipe, zip(*[iterator]*n) to collect the items into groups of 10
  3. print the groups in a table-like format

import itertools as IT

prime_list = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59,
61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139,
149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317,
331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421,
431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521,
523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619,
631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733,
739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839,
853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953,
967, 971, 977, 983, 991, 997]

prime_strs = ('{:<5d}'.format(val) for val in prime_list) # 1
grouped = IT.zip_longest(*[prime_strs]*10, fillvalue='')  # 2
print('n'.join(''.join(row) for row in grouped))         # 3

yields

2    3    5    7    11   13   17   19   23   29   
31   37   41   43   47   53   59   61   67   71   
73   79   83   89   97   101  103  107  109  113  
127  131  137  139  149  151  157  163  167  173  
179  181  191  193  197  199  211  223  227  229  
233  239  241  251  257  263  269  271  277  281  
283  293  307  311  313  317  331  337  347  349  
353  359  367  373  379  383  389  397  401  409  
419  421  431  433  439  443  449  457  461  463  
467  479  487  491  499  503  509  521  523  541  
547  557  563  569  571  577  587  593  599  601  
607  613  617  619  631  641  643  647  653  659  
661  673  677  683  691  701  709  719  727  733  
739  743  751  757  761  769  773  787  797  809  
811  821  823  827  829  839  853  857  859  863  
877  881  883  887  907  911  919  929  937  941  
947  953  967  971  977  983  991  997  

Answered By: unutbu