Вопрос

learning python currently and having a bit of a problem. I'm trying to take a line from another subprogram and convert it into separate words that have been stripped of their punctuation besides a few. the output of this program is supposed to be the word and the line numbers it shows up on. Should look like this -> word: [1]

input file:

please. let! this3 work.
I: hope. it works
and don't shut up

Code:

    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

output:

anddon't:[3]
anddon'tshut:[3]
anddon'tshutup:[3]
ihope:[2]
ihopeit:[2]
ihopeitworks:[2]
pleaselet:[1]
pleaseletthis3:[1]
pleaseletthis3work:[1]

I'm unsure what this is caused by but I learned Ada and transitioning to python in a short period of time.

Это было полезно?

Решение

You should set cleanWord back to an empty string at the top of each iteration of the outer loop:

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

Note that I also removed the if word != " ", since after line.split() you will never have spaces.

>>> createWordList('please. let! this3 work.')
['please', 'let', 'this', 'work']

Другие советы

Of course, you could also use a regular expression:

>>> import re
>>> s = """please. let! this3 work.
... I: hope. it works
... and don't shut up"""
>>> re.findall(r'[^\s!,.?":;0-9]+', s)
['please', 'let', 'this', 'work', 'I', 'hope', 'it', 'works', 'and', "don't", 
 'shut', 'up']
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top