将字符串转换为单词列表?

| 我正在尝试使用python将字符串转换为单词列表。我想采取以下措施:
string = \'This is a string, with words!\'
然后转换成这样的东西:
list = [\'This\', \'is\', \'a\', \'string\', \'with\', \'words\']
注意省略了标点符号和空格。最快的解决方法是什么?     
已邀请:
        尝试这个:
import re

mystr = \'This is a string, with words!\'
wordList = re.sub(\"[^\\w]\", \" \",  mystr).split()
怎么运行的: 从文档:
re.sub(pattern, repl, string, count=0, flags=0)
返回通过用替换repl替换字符串中最左边的非重叠模式所获得的字符串。如果找不到该模式,则字符串将保持不变。 repl可以是字符串或函数。 所以在我们的情况下: 模式是任何非字母数字字符。 [\\ w]表示任何字母数字字符,并且等于该字符集 [a-zA-Z0-9_] a到z,A到Z,0到9并加下划线。 因此,我们匹配任何非字母数字字符并将其替换为空格。 然后我们将split()拆分成空格,然后将其转换为列表 所以\'hello-world \' 成为“你好世界” 带re.sub 然后[\'hello \',\'world \'] 在split()之后 让我知道是否有任何疑问。     
        我认为这是其他任何人在最迟收到回复的情况下绊倒这篇文章的最简单方法:
>>> string = \'This is a string, with words!\'
>>> string.split()
[\'This\', \'is\', \'a\', \'string,\', \'with\', \'words!\']
    
        正确执行此操作非常复杂。为了您的研究,它被称为单词标记化。如果您想看看别人做了什么,而不是从头开始,应该看看NLTK:
>>> import nltk
>>> paragraph = u\"Hi, this is my first sentence. And this is my second.\"
>>> sentences = nltk.sent_tokenize(paragraph)
>>> for sentence in sentences:
...     nltk.word_tokenize(sentence)
[u\'Hi\', u\',\', u\'this\', u\'is\', u\'my\', u\'first\', u\'sentence\', u\'.\']
[u\'And\', u\'this\', u\'is\', u\'my\', u\'second\', u\'.\']
    
        最简单的方法:
>>> import re
>>> string = \'This is a string, with words!\'
>>> re.findall(r\'\\w+\', string)
[\'This\', \'is\', \'a\', \'string\', \'with\', \'words\']
    
        使用
string.punctuation
表示完整性:
import re
import string
x = re.sub(\'[\'+string.punctuation+\']\', \'\', s).split()
这也处理换行符。     
        好吧,你可以使用
import re
list = re.sub(r\'[.!,;?]\', \' \', string).split()
请注意,
string
list
都是内置类型的名称,因此您可能不想将它们用作变量名。     
        受@mtrw的答案启发,但经过改进,仅在单词边界处去除了标点符号:
import re
import string

def extract_words(s):
    return [re.sub(\'^[{0}]+|[{0}]+$\'.format(string.punctuation), \'\', w) for w in s.split()]

>>> str = \'This is a string, with words!\'
>>> extract_words(str)
[\'This\', \'is\', \'a\', \'string\', \'with\', \'words\']

>>> str = \'\'\'I\'m a custom-built sentence with \"tricky\" words like https://stackoverflow.com/.\'\'\'
>>> extract_words(str)
[\"I\'m\", \'a\', \'custom-built\', \'sentence\', \'with\', \'tricky\', \'words\', \'like\', \'https://stackoverflow.com\']
    
        单词的正则表达式将使您获得最大的控制权。您可能需要仔细考虑如何处理带有破折号或撇号的单词,例如\“ I \ m \”。     
        我个人认为,这比提供的答案还干净
def split_to_words(sentence):
    return list(filter(lambda w: len(w) > 0, re.split(\'\\W+\', sentence))) #Use sentence.lower(), if needed
    
        
list=mystr.split(\" \",mystr.count(\" \"))
    
        这是由于我尝试了无法使用正则表达式的编码挑战,
outputList = \"\".join((c if c.isalnum() or c==\"\'\" else \' \') for c in inputStr ).split(\' \')
撇号的作用似乎很有趣。     
        这样,您就可以消除字母之外的所有特殊字符:
def wordsToList(strn):
    L = strn.split()
    cleanL = []
    abc = \'abcdefghijklmnopqrstuvwxyz\'
    ABC = abc.upper()
    letters = abc + ABC
    for e in L:
        word = \'\'
        for c in e:
            if c in letters:
                word += c
        if word != \'\':
            cleanL.append(word)
    return cleanL

s = \'She loves you, yea yea yea! \'
L = wordsToList(s)
print(L)  # [\'She\', \'loves\', \'you\', \'yea\', \'yea\', \'yea\']
我不确定这是快速的还是最佳的,甚至是正确的编程方式。     
        您可以尝试执行以下操作:
tryTrans = string.maketrans(\",!\", \"  \")
str = \"This is a string, with words!\"
str = str.translate(tryTrans)
listOfWords = str.split()
    

要回复问题请先登录注册