简体   繁体   中英

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. 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
  • Return listwords after the loop
  • 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 **

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. 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.

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]

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