【发布时间】:2021-04-17 06:46:45
【问题描述】:
如何使用列表理解去除字符串中的标点符号?
punctuations="!@#$%^&*()_-=+:;{}[]<>,.?/\''"
analyzed=""
text="This is ;;;; $# @#%@$ A String <>?::"
我知道如何使用 For 循环:
for i in text:
if i not in punctuations:
analyzed+=i
print(analyzed)
但是如何使用列表理解来做到这一点?
【问题讨论】:
-
你尝试了什么?
-
我试过了:print([analyzed+=i for i in punctuations if i in text])
-
使用带有条件的列表推导,并将其作为参数传递给
''.join() -
你倒退了。
for i in text if i not in punctuations -
i打印的是什么?你不想要analyzed吗?
标签: python arrays list list-comprehension