找出已放入列表的句子中的所有标点符号

时间:2016-10-08 16:26:25

标签: python list punctuation

我有这个变量:

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

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

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

使用re.findall函数的解决方案:

import re

Words = ["Hi",".","how","are","you","?","I","feel","like","I","could",",","do","better"]
Punctuations = re.findall("[^\w\s]+", ''.join(Words))

print(Punctuations)  # ['.', '?', ',']