[英]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. 我正在写一个lengthWords函数,该函数返回一个字典,该字典将单词的长度映射到对应于该特定长度的句子中的单词列表。
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? 为什么“香蕉”不在长度6类别中?
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. 这将是一种更好的方法,因为它会检查字典中当前是否存在for
循环的单词的长度。 wordDictionary[len(word)] = [x for x in string if len(x) == len(word)]
is a list of all the words with that particular length wordDictionary[len(word)] = [x for x in string if len(x) == len(word)]
是具有该特定长度的所有单词的列表
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. if i == number:
要计算单词的长度而不是它的位置,则需要用if len(string[i])==number:
替换if i == number:
if len(string[i])==number:
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: 另外,您可以使用defaultdict ,它使您可以删除第一个循环:
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."))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.