小编Pro*_*tor的帖子

简洁的标点符号并分成单词python

学习python目前还有一点问题.我试图从另一个子程序中取一条线并将其转换为单独的单词,除了少数几个之外,它们已经被删除了标点符号.该程序的输出应该是它显示的单词和行号.应该是这样的 - >字:[1]

输入文件:

please. let! this3 work.
I: hope. it works
and don't shut up
Run Code Online (Sandbox Code Playgroud)

码:

    def createWordList(line):
        wordList2 =[]
        wordList1 = line.split()
        cleanWord = ""
        for word in wordList1: 
            if word != " ":
                for char in word:
                    if char in '!,.?":;0123456789':
                        char = ""
                    cleanWord += char
                    print(cleanWord," cleaned")
                wordList2.append(cleanWord)
         return wordList2
Run Code Online (Sandbox Code Playgroud)

输出:

anddon't:[3]
anddon'tshut:[3]
anddon'tshutup:[3]
ihope:[2]
ihopeit:[2]
ihopeitworks:[2]
pleaselet:[1]
pleaseletthis3:[1]
pleaseletthis3work:[1]
Run Code Online (Sandbox Code Playgroud)

我不确定这是由什么引起的,但我学会了Ada并在短时间内转换到python.

python strip line cpu-word punctuation

4
推荐指数
2
解决办法
6189
查看次数

标签 统计

cpu-word ×1

line ×1

punctuation ×1

python ×1

strip ×1