简体   繁体   中英

How to append more than one word?

I am writing a function lengthWords that returns a dictionary that maps a the length of a word to a list of words from a sentence corresponding to that specific length.

Right now this is my code:

def lengthWords(string):
    string = string.lower()
    string = string.replace(".", "")
    string = string.replace(",", "")
    string = string.replace("?", "")
    string = string.replace("!", "")
    string = string.split()

    wordDictionary = {}
    for word in string:
        number = len(word)
        wordDictionary[number] = []
        for i in range(0,len(string)):
            if i == number:
                wordDictionary[number].append(word)

return wordDictionary 


print(lengthWords("I ate a banana."))

Right now my output is

{1: ['a'], 3: ['ate'], 6: []}

When I really need is

{1: ['i', 'a'], 3:['ate'], 6:['banana']}

What part of my code should I fix? and why isn't "banana" in the length 6 category?

def lengthWords(string):
    non = ['.',',','?','!']
    string = string.lower()
    for x in string:
        if x in non:
            string = string.replace(x,'')
    string = string.split()

    wordDictionary = {}
    for word in string:
        if len(word) not in wordDictionary:
            wordDictionary[len(word)] = [x for x in string if len(x) == 
len(word)]


    return wordDictionary 

This would be a much better approach as it checks if the length of the word the for loop is currently on, is in the dictionary. wordDictionary[len(word)] = [x for x in string if len(x) == len(word)] is a list of all the words with that particular length

You need to replace if i == number: with if len(string[i])==number: , as you want to calculate the length of the word, not it's position. Also, it is better to take the second loop outside the first one, to reduce complexity:

def lengthWords(string):
    string = string.lower()
    string = string.replace(".", "")
    string = string.replace(",", "")
    string = string.replace("?", "")
    string = string.replace("!", "")
    string = string.split()

    wordDictionary = {}
    for word in string:
        number = len(word)
        wordDictionary[number] = []
    for i in range(0,len(string)):
        wordDictionary[len(string[i])].append(string[i])

    return wordDictionary 


print(lengthWords("I ate a banana."))

In addition, you can use defaultdict , which enables you to remove the first loop:

from collections import defaultdict

def lengthWords(string):
    string = string.lower()
    string = string.replace(".", "")
    string = string.replace(",", "")
    string = string.replace("?", "")
    string = string.replace("!", "")
    string = string.split()

    wordDictionary = defaultdict(list)
    for i in range(0,len(string)):
        wordDictionary[len(string[i])].append(string[i])

    return wordDictionary 


print(lengthWords("I ate a banana."))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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