python - 在不使用 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)
解决方案
希望这可以帮助你:
lst = ["is ", "star,", "the-"]
lst = [''.join(e for e in f if e.isalpha()) for f in lst]
print(lst)
输出:
['is', 'star', 'the']
推荐阅读
- c# - 如何在 C# 中使用线程安全列表?
- jquery - 调用添加分类 jquery
- node.js - Angular 您可能需要适当的加载程序错误
- php - Woocommerce - 在 CHECKOUT 中模拟“运送到不同的地址?”以更新订单货币
- dart - DropdownButton 没有改变 onChanged 的值
- javascript - 在 MongoDB 查找函数中,我回调并使用 res.redirect,我得到一个无法设置标头错误?
- c# - 引入 FOREIGN KEY 约束“XXX”......无法创建约束或索引
- java - Task-Runnable-、Thread-、Timeline 对象是线程安全的吗?
- r - 如何在公式中使用带有非二进制变量的 prodlim 函数?
- api - Symfony 4.2 和 Nelmio Api Doc Bundle - 带有多个控制器的几个文档