[英]Write a function filter_long_words() that takes a list of words and an integer n and returns the list of words that are longer than n
Whenever I run this code it just gives me a blank list, I am wondering what I am doing wrong.每当我运行此代码时,它只会给我一个空白列表,我想知道我做错了什么。 I am trying to print a list of words that are longer than n.我正在尝试打印一个比 n 长的单词列表。 When i try to run the updated code it only prints the first word from the list of words that i enter.当我尝试运行更新后的代码时,它只打印我输入的单词列表中的第一个单词。
def filterlongword(string,number):
for i in range(len(string)):
listwords = []
if len(string[i]) > number:
listwords.append(string[i])
return listwords
def main():
words = input("Please input the list of words: ")
integer = eval(input("Please input an integer: "))
words1 = filterlongword(words,integer)
print("The list of words greater than the integer is",words1)
main()
listwords
before the loop初始化listwords
循环之前listwords
after the loop返回listwords
循环后def filterlongword(string,number):
listwords = []
for i in range(len(string)):
if len(string[i]) > number:
listwords.append(string[i])
return listwords
And a nicer version using list comprehension :并使用更好的版本列表理解:
def filterlongword(string,number):
return [word for word in string if len(word) > number]
To split the input string into a list of words, use要输入字符串分割成单词列表,使用
words = input("Please input the list of words: ").split()
even better would be just更妙的是刚
def filterlongword(string,number):
return filter(lambda word:len(word)>number, string)
# or: return [w for w in string if len(w) > number]
def listing(guess, number):
new_list = []
for i in range(len(guess)):
if len(guess[i]) > number:
new_list.append(guess[i])
print (new_list)
list1 = input("take input: ")
list = list1.split(",")
def main():
global list, integer1
integer = input()
integer1 = int(integer)
listing(list, integer1)
main()
**try this code..this will work, use a delimiter to form a list of your input ** **试试这个code..this将工作,使用分隔符来形成你的输入列表**
Your main problem is passing words as a single string rather than an iterable of strings.你的主要问题是经过词作为一个字符串,而不是字符串的迭代。 The secondary problem is not specifying the separator between words for the missing .split.次级问题不指定单词之间的分离器,用于缺少.split。 Here is my version.这是我的版本。
I made longwords
a generator function because in actually use, one does not necessary need the sequence of long words to be a list, and I gave an example of this in the output formatting.我把longwords
做成了一个生成器函数,因为在实际使用中,不需要长词的序列成为一个列表,我在输出格式中给出了一个例子。
def longwords(wordlist, length):
return (word for word in wordlist if len(word) >= length)
def main():
words = input("Enter words, separated by spaces: ").split()
length = int(input("Minimum length of words to keep: "))
print("Words longer than {} are {}.".format(length,
', '.join(longwords(words, length))))
main()
This results in, for instance这导致,例如
Enter words, separated by spaces: a bb ccc dd eeee f ggggg
Minimum length of words to keep: 3
Words longer than 3 are ccc, eeee, ggggg.
Maybe you can shorten the code to the following:也许您可以将代码缩短为以下内容:
def filter_long_words():
n = raw_input("Give words and a number: ").split()
return sorted(n)[1:] # sorted the List , number it is the shorter .
# called the item from the second position to ende .
print filter_long_words()
def filter_long_words(**words**,**number**):
l=[]
split = **words**.split(",")
for i in split:
if len(i) > **number**:
l.append(i)
return l
**w** = input("enter word:")
**n** = int(input("Enter number"))
filter_long_words(**w**,**n**)
TRY THIS尝试这个
***
def filter_long_words(n, words):
list_of_words=[]
a= words.split(" ")
for x in a:
if len(x)>n:
list_of_words.append(x)
return list_of_words
for i in range(len(listOfWords)):
listOfInt = []
for i in range(len(listOfWords)):
listOfInt.append(len(listOfWords[i]))
print('List of word length: ',listOfInt)
def filter_long_words(lst,n):
a=[]
for i in lst:
if n<len(i):
a.append(i)
return a
To filter list of words要过滤的单词列表
def filter_long_words(lst,n):
return [word for word in lst if len(word)>n]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.