找出已放入列表的句子中的所有标点符号
Find out all the punctuations in a sentence that has been put into a list
我有这个变量:
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("[^\w\s]+", ''.join(Words))
print(Punctuations) # ['.', '?', ',']
我有这个变量:
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("[^\w\s]+", ''.join(Words))
print(Punctuations) # ['.', '?', ',']