简体   繁体   中英

Can't figure out this filter() recursion error

The problem is I have to write a program which takes in a list of words and an integer and returns the words whose length is longer than that integer. I have to use filter() only. This is what I wrote :

def filter(list,integer):
    largenum = list(filter(lambda x: len(x) > integer, list ))
    return largenum
inp = input("Enter the words: ").split()
intr = input("Enter the integer: ").split()

print (filter(inp,intr))

When I run this and give the inputs, it gives an error:

Runtime error: Maximum recursion depth exceeded.

What am I doing wrong?

edit: I got it. Such a silly mistake(s) XD. 1.) I changed filter(list,integet) to filterthis(string,integer) 2.) intr = input("Enter the integer: ").split() to intr = int(input("Enter the integer: ")

You have written filter function which calls itself without a base case.

Rename your filter function.

In [8]: def my_filter(l, i):                                                                                                        
   ...:     largenum = filter(lambda x: len(x)> i, l)  # if python3, use list(filter)                                                                        
   ...:     return largenum                                                                                                         
   ...:                                                                                                                             

In [9]: inp = ["LOL", "DA", "YAYASTRING"]                                                                                                                     

In [10]: intr = 2                                                                                                                   

In [11]: my_filter(inp, intr)                                                                                                       
Out[11]: ['LOL', 'YAYASTRING']                                                                                                       

You are passing integer as list .So use integer[0] .Then input returns str .So use int(integer[0]) .

Then you are using filter as your function name .So this will override the builtin function filter .Also you are passing your list as variable list .It will also override the builtin callable list .You can try this

def myfilter(mylist,integer):
    largenum = list(filter(lambda x: len(x) > int(integer[0]), mylist ))
    return largenum
inp = input("Enter the words: ").split()
intr = input("Enter the integer: ").split()

>>>print(myfilter(inp,intr))

Your version of filter will shadow the python built-in which has the same name. So when you make a call to it from inside your filter , it's not really to the built-in you are intending to call, but to your function itself. Since there is no stopping rule for the recursion, it ends up exceeding permissible stack depth.

Same goes for list . The function argument with the same name will shadow the builtin python list container.

Also, you'll need to cast the second argument to int before passing it to the function.

For code:

def fil(lst, integer):
    return filter(lambda x: len(x) > integer, lst)

>>> fil(['Hello', 'how', 'are', 'you', 'doin'], 3)
['Hello', 'doin']

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