-1

可以说我有一个

[["Hello, world!"],["Hello!!, WORLD!!"]]

我希望它产生

[["Hello","world"],["Hello","WORLD"]]
4

3 回答 3

4

我会使用正则表达式:

>>> import re
>>> text = "Hello!!, WORLD!!"
>>> re.findall(r'\w+', text)
['Hello', 'WORLD']
于 2013-03-25T01:10:08.017 回答
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)
于 2013-03-25T01:14:11.737 回答
0

不使用正则表达式的版本:

import string

def remove_punctuation_and_split(word):
    return word.translate(None, string.punctuation).split()

remove_punctuation_and_split('Hello, world!!')
['Hello', 'world']
于 2013-03-25T01:20:04.377 回答