Python 如何打印10列整数列表?
我知道这可能非常简单,但我正在努力。我想把这张素数列表打印在对齐的列中,每行有10个素数 我的程序目前将所有数字打印在一行中Python 如何打印10列整数列表?,python,list,python-3.x,Python,List,Python 3.x,我知道这可能非常简单,但我正在努力。我想把这张素数列表打印在对齐的列中,每行有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) pri
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=' ')
请帮帮我。谢谢大家! 这个最简单的方法就是在打印(*prime,sep='')的
末尾迭代prime
如果您正在使用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
编辑:
如果使用的是Python 3,则需要更改:
print "{:3}".format(p),
到
您将需要更改新行的打印位置
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 ()
当我运行这个程序时,我得到一个“{:3d}”的语法错误。我不知道为什么。我也尝试过在没有d的情况下运行它。您正在运行什么版本的Python?3.4.3谢谢您的帮助@迈克尔雷
# 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 ()