首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用backspace删除字符

使用backspace删除字符
EN

Stack Overflow用户
提问于 2021-07-08 02:13:59
回答 1查看 337关注 0票数 1

我正在创建一个文本框类,其中包含一个更新其文本的方法。它以用户的键盘输入作为参数,keys。文本框类还具有一个属性text,它非常不言自明,它是它的文本。因此,我需要将用户输入添加到文本框的text中。除非用户输入一个退空格键(即'\x08'),否则我需要删除它前面的所有字母。但是当然,如果有两个连续的后置空间,这不像一个backspace会删除一个backspace,我需要删除两次。因此,现在您已经了解了情况,下面是我解决问题的方法:

代码语言:javascript
复制
        def update(self, keys):
            if len(keys) > 0:
                
                # I have a shift variable because when you modify a variable at the same time as
                # iterating through it, the index will off shift, so I need to reverse that
                shift = 0
                for i in range(len(keys)):
                    i -= shift
                    
                    # If there's a backspace
                    if keys[i] == '\x08':
                        # If it's the first character
                        if i == 0:
                            # Remove the last character from the text box's text
                            self.text = self.text[:-1]
                            # Get rid of the backspace from the user input
                            keys = keys[1:]
                            # reverse the off shift, or else the for loop will skip the second element
                            shift += 1
                            
                        else:
                            j = i
                            # Counts how many backspaces follow up consecutively
                            del_count = 1
                            while keys[j] != '\x08':
                                j -= 1
                                del_count += 1

                            # Adjusts the user's input
                            keys = keys[:j - del_count] + keys[i+1:]
                            shift += del_count
                      
                    
                self.text += keys
                print(self.text)

它几乎可以很好地工作,除非用户的输入是一个由多个后置空间跟踪的字符,否则我会得到一个索引错误。例如,如果用户输入(keys)是"a\x08\x08",我将在if keys[i] == '\x08':上得到一个索引错误。这是什么原因?有人能给我一个解决办法吗?提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-08 03:06:01

您不应该修改其循环中的可迭代性。

我在三个步骤中遇到了这个问题:

  • 合并了textkeys first
  • 计数和收集后台字符索引,并实现了普通字符
  • 在合并对象中以反序

删除了这两种字符。

试试这个:

代码语言:javascript
复制
def update(text, keys):
    if keys:
        # Step 1: Merge both `key` and `texts`
        print(repr(text))
        text += keys
        print(repr(text))

        # Step 2: Count and collect `backspace` posision and `effected chars` position in reversed order
        backspace_posision = []
        char_deleted_position = []
        for i, char in enumerate(text[::-1]):
            _r_i = - i - 1

            if char == "\x08":
                backspace_posision.append(_r_i)
                print("backspace_posision:", backspace_posision)
            elif len(char_deleted_position) < len(backspace_posision):
                char_deleted_position.append(_r_i)
                print(char)
                print("char_deleted_position:", char_deleted_position)

        # Step 3: Remove them in reversed order
        texts = list(text)
        text_leng = len(texts)
        
        print(repr(texts))
        for _r_i in sorted(backspace_posision + char_deleted_position, reverse=True):
            i = text_leng + _r_i
            print(i)
            texts.pop(i)
            print(repr(texts))

        text = "".join(texts)
        print(repr(text))

示例输出如下所示

代码语言:javascript
复制
update("ABC", "D\x08\x08EFG\x08H\x08\x08")

# Original text
>>>'ABC'
# Merged
>>>'ABCD\x08\x08EFG\x08H\x08\x08'

# Collecting backspace and effected chars in reversed order
# Collecting backspace
>>>backspace_posision: [-1]
>>>backspace_posision: [-1, -2]

# Collecting effected chars
>>>H
>>>char_deleted_position: [-3]

# Collecting backspace
>>>backspace_posision: [-1, -2, -4]

# Collecting effected chars
>>>G
>>>char_deleted_position: [-3, -5]
>>>F
>>>char_deleted_position: [-3, -5, -6]

# Collecting backspace
>>>backspace_posision: [-1, -2, -4, -8]
>>>backspace_posision: [-1, -2, -4, -8, -9]

# Collecting effected chars
>>>D
>>>char_deleted_position: [-3, -5, -6, -10]
>>>C
>>>char_deleted_position: [-3, -5, -6, -10, -11]

# Iterate the merged text in reversed order and remove them
>>>['A', 'B', 'C', 'D', '\x08', '\x08', 'E', 'F', 'G', '\x08', 'H', '\x08', '\x08']
>>>12
>>>['A', 'B', 'C', 'D', '\x08', '\x08', 'E', 'F', 'G', '\x08', 'H', '\x08']
>>>11
>>>['A', 'B', 'C', 'D', '\x08', '\x08', 'E', 'F', 'G', '\x08', 'H']
>>>10
>>>['A', 'B', 'C', 'D', '\x08', '\x08', 'E', 'F', 'G', '\x08']
>>>9
>>>['A', 'B', 'C', 'D', '\x08', '\x08', 'E', 'F', 'G']
>>>8
>>>['A', 'B', 'C', 'D', '\x08', '\x08', 'E', 'F']
>>>7
>>>['A', 'B', 'C', 'D', '\x08', '\x08', 'E']
>>>5
>>>['A', 'B', 'C', 'D', '\x08', 'E']
>>>4
>>>['A', 'B', 'C', 'D', 'E']
>>>3
>>>['A', 'B', 'C', 'E']
>>>2
>>>['A', 'B', 'E']
>>>'ABE'
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68294804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档