2015-11-30 3 views
0

Я знаю, что это может быть очень просто, но я борюсь. Я хотел бы напечатать этот список простых чисел в выровненных столбцах с 10 простыми числами в каждой строке.Как распечатать список целых чисел в 10 столбцах?

В настоящее время моя программа печатает все цифры в одной строке.

prime = [] 
not_prime = [] 

for i in range(2,numbers+1): 

    if i not in not_prime: 
     prime.append(i) 

     for x in range(i**2,numbers+1,i): 
      not_prime.append(x) 

print (*prime, sep=' ') 

Пожалуйста, помогите мне. Спасибо!

ответ

2

Этот простейший подход состоял бы в том, чтобы просто пройти через prime в конце, где у вас есть print (*prime, sep=' ').

Если вы используете Python 2:

# use `numbers = 100` as an example 
numbers = 100 

prime = [] 
not_prime = [] 

for i in range(2,numbers+1): 

    if i not in not_prime: 
     prime.append(i) 

     for x in range(i**2,numbers+1,i): 
      not_prime.append(x) 

# `enumerate` gives us a tuple of (index, element) in an iterable 
for idx, p in enumerate(prime): 

    # "{:3d}" is a format string that is similar to the C-style 
    # of %X.YA where `X` denotes the character width, `.Y` denotes 
    # how many places to display in a floating point number, 
    # and `A` denotes the type of what's being printed. Also note, 
    # in Python, you don't need to use the `d` in `:3d`, you can just 
    # do `{:3}`, but I've included it for your knowledge. 
    # 
    # the comma says 'don't add a newline after you print this' 
    print "{:3d}".format(p), 

    # we'll use `idx + 1` to avoid having to deal with the 
    # 0-indexing case (i.e., 0 modulo anything is 0) 
    if (idx + 1) % 10 == 0: 

     # just print a newline 
     print 

Результат:

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 

Edit:

Если вы используете Python 3, вы» хочу изменить:

print "{:3}".format(p), 

в

print ("{:3}".format(p), end="") 

и вы хотите изменить, где вы печатаете символ новой строки в

print() 

Полученный код поэтому:

# use `numbers = 100` as an example 
numbers = 100 

prime = [] 
not_prime = [] 

for i in range(2,numbers+1): 

    if i not in not_prime: 
     prime.append(i) 

     for x in range(i**2,numbers+1,i): 
      not_prime.append(x) 

# `enumerate` gives us a tuple of (index, element) in an iterable 
# `start=1` tells enumerate to use a 1-based indexing rather than 
# 0-based. 
for idx, p in enumerate(prime, start=1): 

    # "{:3d}" is a format string that is similar to the C-style 
    # of %X.YA where `X` denotes the character width, `.Y` denotes 
    # how many places to display in a floating point number, 
    # and `A` denotes the type of what's being printed. Also note, 
    # in Python, you don't need to use the `d` in `:3d`, you can just 
    # do `{:3}`, but I've included it for your knowledge. 
    # 
    # the comma says 'don't add a newline after you print this' 
    print ("{:3d}".format(p), end="") 

    # if it's a multiple of 10, print a newline 
    if idx % 10 == 0: 

     # just print a newline 
     print() 
+0

Когда я запустите программу, я получаю синтаксическую ошибку для «{: 3d}». Я не знаю, почему. Я попытался запустить его без d. –

+0

Какую версию Python вы используете? –

+0

3.4.3 Спасибо за помощь! –

Смежные вопросы