【问题标题】:creating new classes in a loop according to user input根据用户输入循环创建新类
【发布时间】:2017-07-23 03:53:13
【问题描述】:

所以我正在尝试制作一个质数筛,它将生成一个类对象,该对象将质数存储到用户给出的数字。但我想在一个循环中运行,用户可以继续创建存储有素数列表的新对象,并让他们可以选择继续这样做。

如果他们两次输入相同的数字,我只想打印之前创建的对象中存储的先前结果,而不是替换先前的对象并运行建立一个全新列表的计算。

  1. 我认为唯一让我感到困惑的是如何不断地创建新的类对象并每次都给对象新的名称,而不是我第一次做的,将对象存储在变量中'C'。

如果你看我的代码,筛子会正常工作,我只是不知道如何制作一个对象而不将它存储在变量'c'中

history={}
class Eratosthenes(object):

    def __init__(self,b):
        self.limitn=int(b)+1
        self.not_prime = [False]*self.limitn
        self.primes = []

    def do_primes_sieve(self):
        for i in range(2, self.limitn):
            if self.not_prime[i]:
                continue
            for f in xrange(i**2, self.limitn, i):
                self.not_prime[f] = True
            self.primes.append(i)

    def get_primes_til(self):
        for i in self.primes:
            print (i)
    def get_num_of_primes(self):
        print (len(self.primes))

a = True
while a:
    b = raw_input("How far are you going to search?: ")
    if b in history.keys():
        c = history[b]
        c.get_num_of_primes
    else:
        c = Eratosthenes(b)
        history[b] = c
        c.do_primes_sieve()
        c.get_primes_til()
    d = raw_input("Continue? (type 'yes' or 'no'): ")
    if d == "yes":
        continue
    elif d == "no":
        a=False
    else:
        print "This is a 'yes' or 'no' question. We're done here."
        a=False

【问题讨论】:

  • 一个类对象?
  • 是一个类对象
  • 你关心 old 实例吗?你所拥有的有什么问题?
  • 我的目标是创建一个新实例,这样我就可以在适当的位置创建实例,用户只需再次输入他们的号码就可以调用它们。
  • 将实例作为键/值对添加到字典中 - number:instance

标签: python class object sieve-of-eratosthenes


【解决方案1】:

你可以声明一些类变量(又名静态变量),例如一个名为history 的字典,它将存储结果:

class Eratosthenes(object):

    history = {}

    def __init__(self,b):
        self.limitn=int(b)+1
        self.not_prime = [False]*self.limitn
        self.primes = []

    def do_primes_sieve(self):
        if self.limitn-1 in Eratosthenes.history.keys():
            self.primes = Eratosthenes.history[self.limitn-1]
        else:
            for i in range(2, self.limitn):
                if self.not_prime[i]:
                    continue
                for f in xrange(i**2, self.limitn, i):
                    self.not_prime[f] = True
                self.primes.append(i)
            Eratosthenes.history[self.limitn-1] = self.primes

然后,您可以修改您的do_primes_sieve 方法,以便如果history 字典中存在先前的答案,则将从字典中获取Eratosthenes 的当前实例的primes

根据下面的讨论,将history 视为全局的更新答案:

history = {}

class Eratosthenes(object):


    def __init__(self,b):
        self.limitn=int(b)+1
        self.not_prime = [False]*self.limitn
        self.primes = []

    def do_primes_sieve(self):
        for i in range(2, self.limitn):
            if self.not_prime[i]:
                continue
            for f in range(i**2, self.limitn, i):
                self.not_prime[f] = True
            self.primes.append(i)

    def get_primes_til(self):
        for i in self.primes:
            print i 
    def get_num_of_primes(self):
        print len(self.primes)
a = True
while a:
    b = raw_input("How far are you going to search?: ")
    if b in history.keys():
        c = history[b]
        c.get_num_of_primes()
    else:
        c = Eratosthenes(b)
        history[b] = c
        c.do_primes_sieve()
        c.get_primes_til()
    d = raw_input("Continue? (type 'yes' or 'no'): ")
    if d == "yes":
        continue
    elif d == "no":
        a=False
    else:
        print "This is a 'yes' or 'no' question. We're done here."
        a=False

【讨论】:

  • 我并没有真正挂断这一点,因为我正在弄清楚如何让程序使用新变量创建新的类实例来调用它们。例如,我第二次运行 while 循环时,它会将新的请求实例存储在“c”变量中,而不是新的变量中,从而删除旧实例。
  • 在这种情况下,你可以让history 成为一个全局对象(将它移到类之外,例如移到程序的顶部)。然后,您可以将数字存储为键,将对象存储为值。例如,在c = Eratosthenes(b) 行之后,您可以添加一个新行history[b] = c。您还需要在 while 循环中使用条件来检查 b 是否在 history.keys() 中,如果是,则从 history 字典中获取实例,而不是创建新实例。
  • 这听起来是个好主意。我需要一些时间来设置它。我是设计程序的新手,所以需要一些时间。
  • 如果您遇到任何问题,请随时参考我的更新答案,其中包括(在第二个代码块中)将 history 视为全局的实现。对程序设计的另一条建议:我建议使用描述性名称命名非平凡变量,例如 program_running 而不是 auser_input 而不是 b
  • 那么如果我想调用一个在特定实例上运行的方法,我应该输入什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-07
  • 1970-01-01
  • 2014-11-16
相关资源
最近更新 更多