首页 > 解决方案 > 在不使用 re-Python 的情况下删除列表中除空格外的特殊字符

问题描述

我有列表 lst= ["is ", "star,", "the-"] 并且我想删除 ',', '-' 而不使用 re。

我使用了下面的,它可以工作,但我想知道是否有更简单的东西:

words = []
index = 0
length = 0

for char in lst:
    for i, c in enumerate(char):
        if c.isalpha():
            if length == 0:
                index = i
            length += 1
        else:
            word = char[index:index + length]
            words.append(word)
            length = 0
print(words)

标签: python

解决方案


希望这可以帮助你:

lst = ["is ", "star,", "the-"] 
lst = [''.join(e for e in f if e.isalpha()) for f in lst] 
print(lst)

输出:

['is', 'star', 'the']

推荐阅读