简体   繁体   English

编写一个函数 filter_long_words() 接受一个单词列表和一个整数 n 并返回长度大于 n 的单词列表

[英]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()  
  • Initialize listwords before the loop初始化listwords循环之前
  • Return listwords after the loop返回listwords循环
  • Split the input string into a list of words拆分输入字符串转换成单词的列表
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.

相关问题 将 n 个单词的列表拆分为 n 个单个单词的列表 - Splitting a list of n words into n lists of single words 如何编写一个函数,它接受一个正整数 N 并返回前 N 个自然数的列表 - How to write a function that takes a positive integer N and returns a list of the first N natural numbers Function 接受字符串文本和正数 integer 并将其转换为单词列表 - Function that takes a string text and positive integer and converts it to list of words 编写一个函数,该函数接受一个单词列表并返回最长单词和最长单词的长度 - Write a function that takes a list of words and returns the longest word and length of the longest one 编写一个 function,它以单词列表作为参数,并将每个单词加倍 - Write a function that takes a list of words as a parameter and that double every word 有没有办法替换长于 X 位的单词的最后 N 个字母? - Is there a way to replace the last N letters of words longer than X digits? 删除列表中单词末尾的\\ n及后面的字母 - Delete the \n and following letters in the end of words in a list 列出所有可能的字母为n的单词 - List all possible words with n letters 创建一个接受单词列表的 function,并返回句子中的一组单词 - Creating a function that takes a list of words, and return a set of words in the sentence 编写 function,它接受输入参数 sentence 和 n(整数类型),并返回包含从给定句子生成的 N-gram 的列表 - write function which takes input parameters sentence and n (type integer), and returns a list that contains N-gram generated from the given sentence
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM