假设我有一个
[["Hello, world!"],["Hello!!, WORLD!!"]]
我想让它产生
[["Hello","world"],["Hello","WORLD"]]
我会使用regex:
>>> import re
>>> text = "Hello!!, WORLD!!"
>>> re.findall(r'w+', text)
['Hello', 'WORLD']
word_list = #your word list
punctuation_marks = re.compile(r'[.?!,":;]')
new_word_list = []
for words in word_list:
sub_list = []
for word in words:
w = punctuation_marks.sub("", word)
sub_list.append(w)
new_word_list.append(sub_list)
一个不使用regex的版本:
import string
def remove_punctuation_and_split(word):
return word.translate(None, string.punctuation).split()
remove_punctuation_and_split('Hello, world!!')
['Hello', 'world']