【发布时间】:2019-07-19 16:55:03
【问题描述】:
假设我有一个字符串列表,例如'12hell0',我想将其剥离为'hello'。
我的想法是:
words = ['12hell0','123word', ...]
for word in words:
stripped = ''.join([i for i in word if not i.isdigit()]
我知道这会产生“地狱”,有没有办法使用检查它是否为“0”然后替换为“o”?不知道如何用多个条件构建理解,但使用常规语句会是这样的:
for word in words:
stripped = ''
word.replace('0','o')
for char in word:
if not char.isdigit():
stripped+=char
感谢您的帮助或指导!
【问题讨论】:
-
预期输出是什么?是
['hello', 'word']吗? -
@DeveshKumarSingh 是的。
-
@DeveshKumarSingh 感谢您的回答,但所选答案的格式可以解决我试图自己解决的问题。