可以说我有一个
[["Hello, world!"],["Hello!!, WORLD!!"]]
我希望它产生
[["Hello","world"],["Hello","WORLD"]]
可以说我有一个
[["Hello, world!"],["Hello!!, WORLD!!"]]
我希望它产生
[["Hello","world"],["Hello","WORLD"]]
我会使用正则表达式:
>>> 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)
不使用正则表达式的版本:
import string
def remove_punctuation_and_split(word):
return word.translate(None, string.punctuation).split()
remove_punctuation_and_split('Hello, world!!')
['Hello', 'world']