我编写了以下代码,打印出质数到所需的数字。
def prime_number(number1):
for desired_num in range(1,int(number1)+1):
if desired_num > 1:
for i in range(2,desired_num):
if (desired_num % i) == 0:
break
else:
print(desired_num)
number2 = int(raw_input ("Enter any number :- "))
print "Prime numbers between 1 and", number2, "are :-"
prime_number(number2)
我得到的输出类似于以下内容,对于具有许多数字的列表而言,这变得非常麻烦。
2
3
5
7
我想要的输出类似于以下内容。
[2, 3, 5, 7]
您对如何获得所需的输出有任何建议吗?
答案 0 :(得分:3)
要实现您的目标(已格式化输出),您必须对代码进行一些更改,换句话说,使其变得更“ pythonic”:
import time
from pprint import pprint, pformat
from typing import Iterable
def prime_number(number1):
"""
generator
:rtype: Iterable[int]
:type number1: int
"""
for desired_num in range(1, int(number1) + 1):
if desired_num > 1:
for i in range(2, desired_num):
if (desired_num % i) == 0:
break
else:
yield desired_num
number2 = int(raw_input("Enter any number :- "))
primes = pformat(list(prime_number(number2)))
print("Prime numbers between 1 and {} are :- {}".format(number2, primes))
我在这里使用的是 pipe :pformat(list(prime_number(...)))
生成器->列表[1、2、3]->文本'[1、3,]'。
然后,我将文本和number2
放入格式的字符串中,并发送到打印文件。
如果您是python的新手,并且不熟悉generators
概念,则可以重写prime_number
函数以将简单的.append
使用到列表中,而不是产生它,然后返回整个列表。
答案 1 :(得分:2)
使用good = set(a)
res = []
i = 0
while i < len(b):
x = b[i]
if x in good:
while i < len(b) and b[i] == x: # is?
res.append(x)
i += 1
else:
i += 1
到新列表:
append
答案 2 :(得分:1)
一个简单的想法是将素数列表存储在python内置列表中,然后简单地以您喜欢的格式打印它!就这么简单。
def prime_number(number1):
primes = [] # An empty list here, that will be populated with primes!
for desired_num in range(1,int(number1)+1):
if desired_num > 1:
for i in range(2,desired_num):
if (desired_num % i) == 0:
break
else:
primes.append(desired_num)
print "[" + ",".join(map(str, primes)) + "]"
number2 = int(raw_input ("Enter any number :- "))
print "Prime numbers between 1 and", number2, "are :-"
primes = prime_number(number2)
答案 3 :(得分:1)
如果使用数组存储这些质数,然后将其打印出来,那就更好了。
import time
def prime_number(number1):
primes = []
for desired_num in range(1,int(number1)+1):
if desired_num > 1:
for i in range(2,desired_num):
if (desired_num % i) == 0:
break
else:
primes.append(desire_num)
return primes
number2 = int(raw_input ("Enter any number :- "))
print "Prime numbers between 1 and", number2, "are :-"
print(prime_number(number2))
答案 4 :(得分:0)
您可以采用两种方式进行打印。将其存储在列表中,然后打印或串行打印。
方法1 Use list and then print it as comma separated
:
import time
def prime_number(number1):
output_list = list()
for desired_num in range(1,int(number1)+1):
if desired_num > 1:
for i in range(2,desired_num):
if (desired_num % i) == 0:
break
else:
output_list.append(desired_num)
return output_list
number2 = int(raw_input ("Enter any number :- "))
print "Prime numbers between 1 and", number2, "are :-"
output_list = prime_number(number2)
print (",".join(output_list))
方法2 Print the numbers in function itself serially using end
:
import time
def prime_number(number1):
for desired_num in range(1,int(number1)+1):
if desired_num > 1:
for i in range(2,desired_num):
if (desired_num % i) == 0:
break
else:
print(desired_num,end = " ")
number2 = int(raw_input ("Enter any number :- "))
print "Prime numbers between 1 and", number2, "are :-"
prime_number(number2)