我一直在使用 Python 中的程序(我是一个完整的新手)遇到问题,它不存储来自计算的数据,并且当我觉得它应该保存它时一遍又一遍地执行它。我怎样才能让 Python 保存答案,这样它就不会一遍又一遍地计算程序?
前任:
import prime
def g(x):
i=0
while i<len(prime.sieve(x)):
print str(prime.sieve(x)[i])+' is prime'
i=i+1
这是“主要”模块,以防有人想要编译它:
def sieve(max):
#Takes in a number, and returns all primes between 2 and that number
#Start with all of the numbers
primes = range(2,max+1)
#Start running through each number
for i in primes:
#Start with double the number, and
j = 2
#remove all multiples
while i * j <= primes[-1]:
#As long as the current multiple of the number
#is less than than the last element in the list
#If the multiple is in the list, take it out
if i * j in primes:
primes.remove(i*j)
j=j+1
return primes
无论如何,第一段代码一遍又一遍地计算列表“prime.sieve(x)”,我想保存它以供打印时参考。
谢谢!
罗弗斯