2016-10-08 49 views
0

我有這個變量的標點符號:找出所有的句子已經投入列表

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

從這個我想是找出所有的標點,並把它變成一個列表,以及一個變量。就像這樣:

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

有很多方法可以做到這一點,你做任何嘗試? –

+0

我使用了string.punctuation方法 –

回答

1

您可以使用string.punctuation識別標點符號:

from string import punctuation 
punctuations = [w for w in words if w in punctuation] 
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) # ['.', '?', ',']