0

I have the following code:

class Memoize:
    def __init__(self, f):
        self.f = f
        self.memo = {}
    def __call__(self, *args):
        if not args in self.memo:
            self.memo[args] = self.f(*args)
        return self.memo[args]

@Memoize

def fib(n):

    if n < 2:
        return n
    else:
        return fib(n-1) + fib(n-2)

I am trying to improve my basic programming skills, I have the following problem that I cannot figure out how to do, I want to be able to put all the values of fib(n-1) + fib(n-2) in a list so I can filter the list of values based on other criteria. What is the best way to achieve this? Thanks

4

1 回答 1

3

只是一个简单的列表理解怎么样?

fiblist = [fib(n) for n in xrange(1,n+1)]
于 2013-10-11T15:34:09.847 回答