我有一张单子。我想过滤掉没有最小长度的单词。我试过过滤,但显示出一些错误。我的密码是
def words_to_integer(x,y):
return len(x)> y
print("enter the list of words : ")
listofwords = [ str(x) for x in input().split()] #list of words
minimumlength = print("enter the length ")
z = list(filter(words_to_integer,(listofwords,minimumlength)))
print("words with length greater than ",minimumlength ,"are" ,z )
错误是
z = list(filter(words_to_integer,(listofwords,minimumlength)))
TypeError: words_to_integer() missing 1 required positional argument: 'y'
你应该看看^{} :
partial(words_to_integer, y=minimumlength)
是与words_to_integer
相同的函数,但参数y
固定在minimumlength
当你打这个的时候
python尝试执行以下操作:
它将失败,因为
words_to_integer
接受2个参数,但只给出了一个你可能想要这样的东西:
就像
filter
:或者在另一个答案中使用
partial
你不能那样做。您需要传递一个已经知道minimumlength的函数
一种简单的方法是使用lambda而不是独立函数:
相关问题 更多 >
编程相关推荐