我写了下面的代码,打印出所需数字的素数。
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]
您对如何获得所需的输出有任何建议吗?
如果使用数组存储这些素数,然后将其打印出来可能会更好。
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))
为了实现您的目标(具有格式化输出),您必须对代码进行一些更改,换句话说,使其“更加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))
我在这里使用的是一个管道:pformat(list(prime_number(...)))
generator - > list [1,2,3] - > text'[1,3,]'。然后我将文本和number2
放入格式化的字符串中,并发送到打印。
如果你是python的初学者并且不熟悉generators
概念,你可以重写prime_number
函数,将一个简单的.append
用于列表而不是产生它,然后返回整个列表。
使用append
到新的列表:
import time
def prime_number(number1):
l=[]
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:
l.append(desired_num)
print(l)
number2 = int(raw_input ("Enter any number :- "))
print "Prime numbers between 1 and", number2, "are :-"
start = time.time()
prime_number(number2)
print(time.time() - start)
一个简单的想法是在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)
您可以采用两种方式进行打印。将其存储在列表中,然后连续打印或打印。
方法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)