繁体   English   中英

从python的字符串列表中删除标点符号

[英]Remove punctuation from a list of list of strings in python

可以说我有一个

[["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']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM