让我说我有一个
[["Hello, world!"],["Hello!!, WORLD!!"]]
我希望它能够产生
[["Hello","world"],["Hello","WORLD"]]
答案 0 :(得分:4)
我会使用正则表达式:
>>> import re
>>> text = "Hello!!, WORLD!!"
>>> re.findall(r'\w+', text)
['Hello', 'WORLD']
答案 1 :(得分:0)
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)
答案 2 :(得分:0)
不使用正则表达式的版本:
import string
def remove_punctuation_and_split(word):
return word.translate(None, string.punctuation).split()
remove_punctuation_and_split('Hello, world!!')
['Hello', 'world']