小编Ada*_*amR的帖子

Python list comprehension从列表中删除元素和下一个元素

我想使用列表推导来从字符列表中删除所有出现的'\ x08'(退格字符)我还想在'\ x08'之前删除字符.

我最终得到了一个递归函数调用,但是有一个可读/pythonic单线程会很好.

示例输入:

['a', 't', '+', 'B', 'A', 'D', '\x08', '\x08', '\x08','c', 'o', 'p', 's', '=', '?']
Run Code Online (Sandbox Code Playgroud)

期望的输出:

['a', 't', '+', 'c', 'o', 'p', 's', '=', '?']
Run Code Online (Sandbox Code Playgroud)

万一人们希望看到我当前的解决方案.

def line_parser(self,line):
    if '\x08' in line:
        del line[line.index('\x08') -1]
        del line[line.index('\x08')]
        self.line_parser(line)
    else:
        self.something_else(line)
Run Code Online (Sandbox Code Playgroud)

python regex coding-style list-comprehension

1
推荐指数
1
解决办法
299
查看次数

标签 统计

coding-style ×1

list-comprehension ×1

python ×1

regex ×1