Python:从字符串中获取单个单词

Python: Get single words from a string

我正在尝试在 python 中制作一个字符串分析器。我以此输入为例:

toAnalyze= "Hello!!gyus-- lol\n" 作为输出,我想要这样的东西:

>Output: ['Hello', '!!', 'guys', '--', ' ', 'lol'] 我希望每个 gropus 都按原始顺序排序

我想扫描原始字符串中的所有字符,直到“\n”字符,我想出了这个解决方案:

toAnalyze= "Hello!!gyus-- lol\n"
final = ""
for char in toAnalyze:
    if char != " \n\t" and char != " " and char != "\n" and char != "\n\t":
            final += char
    elif char == " " or char == "\n" or char == "\n\t" or char == " \n\t":
        if not final.isalnum():
            word= ""
            thing = ""
            for l in final:
                if l.isalnum():
                    word += l
                 else:
                    thing += l
            print("word: " + word)
            print("thing: " + thing )

我当前的输出是:

>Output: thing: !!-- word: Hellogyus lol

你有想法吗? 想要的输出:

>Output: ['Hello', '!!', 'guys', '--', ' ', 'lol']

提前致谢,祝您有愉快的一天

我不是 python 人,但想帮助您入门。这是您可以尝试改进的工作解决方案,使其变得更 pythonist:

toAnalyze= 'Hello!!gyus-- lol\n'

word = ''
separator = ''
tokens = []

for ch in toAnalyze:
    if ch.isalnum():
        word += ch
    
    # we met the first character of a separator, so save a word
    if not ch.isalnum() and word:
        tokens.append(word)
        word = ''
        
    # 1. we met the first alphanumeric after a separator, so save the separator or
    # 2. we met a new separator right after another one, also save the old separator
    if ch.isalnum() and separator or separator and separator[-1] != ch:
        tokens.append(separator)
        separator = ''
       
    if not ch.isalnum():
        separator += ch

您的示例的输出是:

['Hello', '!!', 'gyus', '--', ' ', 'lol']