Python - list formatting so that items are not split between lines

I wrote a program to list all primes less than the number the user enters. When I run the program in PyCharm, the conclusion is very pleasant: each number is correctly located on the line, and no numbers are distributed on two lines. However, when I run the program from the command line, the formatting becomes strange, and the numbers at the end of the line are sometimes interrupted.

Here is my code:

import prime_functions as pf

number = int(input("Find primes up to what number? "))
# Save primes to a list.
primes = pf.list_primes(number)

for prime in primes[0:len(primes)-1]:
    print(prime, end=', ')
print(primes[len(primes)-1])

# Print length of primes.
print(f'Number of primes less than {number}: {len(primes)}')

# Pause before exiting.
input()

The function list_primessimply checks if every odd number from three to the user's number is prime and returns a list of all the primes found.

primes (, ), , , for-loops. - Python, ?

+4
4

- :

# Gather primes in some way.
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

# Group the primes.
per_line = 5
grouped_primes = [primes[i:i+per_line] for i in range(0, len(primes), per_line)]

# Print each group.
for group in grouped_primes:
    print(', '.join(str(x) for x in group))

2, 3, 5, 7, 11
13, 17, 19, 23, 29
31

, , ( ) 5 . per_line, , .

, per_line , .

, join() .

" for-loops", .

+1

, , , . , , :

count = 1
items_per_line = 5
for prime in primes[0:len(primes)-1]:
    if count % items_per_line != 0:
        print(prime, end=', ')
    else:
        print(prime, end=',\n')
    count += 1
if count % items_per_line != 0:
    print(primes[len(primes)-1])
else:
    print("\n" + str(primes[len(primes)-1]))

(%) , 5, , . primes = [1, 3, 5, 7, 11, 13, 17] :

1, 2, 3, 5, 7,
11, 13, 17
+1

, :

primes = [3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 47]
N = 5  # the number to print each line

print(
    ''.join(
        map(
            lambda x: " %2d\n" % x[1] if ((x[0]+1)%(N)==0) else " %2d" % x[1],
            enumerate(primes)
        )
    )
)

:

  3  5  7 11 13
 17 19 23 29 31
 37 41 47

%2d , , 2 . , . N (\n). , ('').

: enumerate (, ), x[0] , x[1] .

0

, , . , - - -, .

code.py:

import sys
#import prime_functions as pf  # Decomment this line
from pprint import pprint as pp


def print_list(seq, items_per_line=5, chars_per_item=4):
    l = len(seq)
    for idx, element in enumerate(seq, start=1):
        if (idx and idx % items_per_line == 0) or idx == l:
            end = "\n"
        else:
            end = ", "
        print("{}".format(element).rjust(chars_per_item), end=end)


number = int(input("Find primes up to what number? "))
# Save primes to a list.
primes = list(range(1234, 1275))
#primes = pf.list_primes(number)  # Decomment this line (, and comment the one above)

if not primes:
    print("Empty list returned. Exiting...")
    sys.exit()

print("\n1 - Original method:")
for prime in primes[:-1]:
    print(prime, end=', ')
print(primes[-1])

print("\n2 - Elements one by one (not much of a difference):")
print(*primes)

print("\n3 - pprint:")
pp(primes, compact=True)

print("\n4 - Custom method 0:")
print_list(primes)


# Print length of primes.
print(f'\nNumber of primes less than {number}: {len(primes)}')

# Pause before exiting.
input("\nPress ENTER to exit...")

Output (truncated lines for viewing exaclty , as in my (140 characters) cmd console):

(py36x64_test) e:\Work\Dev\StackOverflow\q47998156>"e:\Work\Dev\VEnvs\py36x64_test\Scripts\python.exe" code.py
Find primes up to what number? 0

1 - Original method:
1234, 1235, 1236, 1237, 1238, 1239, 1240, 1241, 1242, 1243, 1244, 1245, 1246, 1247, 1248, 1249, 1250, 1251, 1252, 1253, 1254, 1255, 1256, 12
57, 1258, 1259, 1260, 1261, 1262, 1263, 1264, 1265, 1266, 1267, 1268, 1269, 1270, 1271, 1272, 1273, 1274

2 - Elements one by one (not much of a difference):
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274

3 - pprint:
[1234, 1235, 1236, 1237, 1238, 1239, 1240, 1241, 1242, 1243, 1244, 1245, 1246,
1247, 1248, 1249, 1250, 1251, 1252, 1253, 1254, 1255, 1256, 1257, 1258, 1259,
1260, 1261, 1262, 1263, 1264, 1265, 1266, 1267, 1268, 1269, 1270, 1271, 1272,
1273, 1274]

4 - Custom method 0:
1234, 1235, 1236, 1237, 1238
1239, 1240, 1241, 1242, 1243
1244, 1245, 1246, 1247, 1248
1249, 1250, 1251, 1252, 1253
1254, 1255, 1256, 1257, 1258
1259, 1260, 1261, 1262, 1263
1264, 1265, 1266, 1267, 1268
1269, 1270, 1271, 1272, 1273
1274

Number of primes less than 0: 41

Press ENTER to exit...
0
source

Source: https://habr.com/ru/post/1691436/


All Articles