Discuss / Python / Fib 切片加step

Fib 切片加step

Topic source

class Fib(object): def __getitem__(self, n): if isinstance(n, int): a, b = 1, 1 for x in range(n): a, b = b, a+b return a

    if isinstance(n, slice):
        start = n.start
        stop = n.stop
        step = n.step
        L = []
        a, b = 1, 1
        for x in range(0, stop, step):
            print(x)
            if x >= start:
                L.append(a)
            n = 0
            while(n < step):
                n += 1
                a, b =b, a+b
        return  L

f = Fib() print(f[3:10:3])


  • 1

Reply