Python:按顺序执行步骤 - python

我试图制作一个完全可切片的斐波那契数列,但我一直陷于切片的阶跃特征。这是我到目前为止的代码:

class Fib:
    def __init__(self, start, end):
        self.start = start
        self.end = end
    def _fib(self, index):
        items = [0,1]
        n = 2
        while n <= index:
            items.append(items[n-1]+items[n-2])
            n += 1
        return items
    def __len__(self):
        return self.end - self.start
    def __reversed__(self):
        items = self._fib(self.end)
        return reversed(items)
    def __count__(self, item):
        items = self._fib(self.end)
        return items.count(item)
    def __getitem__(self, index):
        if isinstance(index, slice):
            return Fib(slice.start, slice.stop-1)
        if index > len(self):
            raise StopIteration
        if index == 0:
            return 0
        if index == 1:
            return 1
        if index < 1:
            return list(reversed(self._fib(self.end)))[abs(index)]
        items = self._fib(index)
        return items.pop()

它似乎可以通过负数进行常规切片,反转和索引。但是,当我尝试使用诸如Fib(0,100)[:: 2]之类的步骤时,出现错误,我很难解释。

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "fibonacci.py", line 22, in __getitem__
    return Fib(slice.start, slice.stop-1)
TypeError: unsupported operand type(s) for -: 'member_descriptor' and 'int'

编辑:
我最终做了这个...

class Fib:
def __init__(self, start, end):
    self.start = start
    self.end = end
def _fib(self, index):
    items = [0,1]
    n = 2
    while n <= index:
        items.append(items[n-1]+items[n-2])
        n += 1
    return items
def __len__(self):
    return self.end - self.start
def __reversed__(self):
    items = self._fib(self.end)
    return reversed(items)
def __count__(self, item):
    items = self._fib(self.end)
    return items.count(item)
def __getitem__(self, index):
    if isinstance(index, slice):
        _start, _stop, _step = (0, self.end, 1)
        if index.start is not None:
            _start = index.start
        if index.stop is not None:
            _stop = index.stop
        if index.step is not None:
            _step = index.step
        abs_stop = _stop
        abs_start = _start
        if _stop < 0:
            abs_stop = self.end + _stop
        if _start < 0:
            abs_start = self.end + _start
        _n = max(abs_start, abs_stop) - 1
        return self._fib(_n)[_start:_stop:_step]
    if index > len(self):
        raise StopIteration
    if index == 0:
        return 0
    if index == 1:
        return 1
    if index < 1:
        return list(reversed(self._fib(self.end)))[abs(index)]
    items = self._fib(index)
    return items.pop()

python参考方案

应该改为return Fib(index.start, index.stop-1)。然后,由于未提供切片的是None,因此应解决此问题,方法是将index.start替换为0(如果是None),并用序列的长度替换index.stop

Python:如何根据另一列元素明智地查找一列中的空单元格计数? - python

df = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice','Jane', 'Alice','Bob', 'Alice'], 'income…

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

如何打印浮点数的全精度[Python] - python

我编写了以下函数,其中传递了x,y的值:def check(x, y): print(type(x)) print(type(y)) print(x) print(y) if x == y: print "Yes" 现在当我打电话check(1.00000000000000001, 1.0000000000000002)它正在打印:<…