找出一个句子中所有的标点符号,这些标点符号被列在一个列表中



我有这样一个变量:

Words = ["Hi",".","how","are","you","?","I","feel","like","I","could",",","do","better"]

我想要一个变量来找到所有的标点符号,并将其放入列表中。这样的:

Punctuations = [".","?",","]

您可以使用string.punctuation来识别标点符号:

from string import punctuation
punctuations = [w for w in words if w in punctuation]

使用re.findall函数的解:

import re
Words = ["Hi",".","how","are","you","?","I","feel","like","I","could",",","do","better"]
Punctuations = re.findall("[^ws]+", ''.join(Words))
print(Punctuations)  # ['.', '?', ',']

相关内容

  • 没有找到相关文章

最新更新