我有这样一个变量:
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) # ['.', '?', ',']