【问题标题】:Is there a more efficient way of implementing this cut method for text editor - Python有没有更有效的方法来为文本编辑器实现这种剪切方法 - Python
【发布时间】:2021-01-17 17:47:53
【问题描述】:

有没有更有效的方法来实现这个方法?

def cut(self, start, end):
        self.clipboard = self.document[start:end]
        self.document = self.document[:start] + self.document[end:]

【问题讨论】:

  • document 的类型是什么?
  • 不,切片效率很高
  • 剪贴板和文档都是字符串

标签: python performance memory-management


【解决方案1】:

您可以通过直接删除所选片段来避免接触:

def cut(self, start, end):
        self.clipboard = self.document[start:end]
        del self.document[start:end]

【讨论】:

  • TypeError: 'str' 对象不支持删除项目
  • 是的,我没有看到你对document type 的回答
猜你喜欢
  • 1970-01-01
  • 2021-11-15
  • 1970-01-01
  • 2012-01-11
  • 1970-01-01
  • 2021-09-28
  • 2013-10-26
  • 2020-04-05
  • 2011-11-08
相关资源
最近更新 更多