我有一个单词表。我想过滤掉没有最小长度的单词。我尝试过滤器,但显示了一些错误。我的代码是

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'

最佳答案

您应该查看functools.partial

from functools import partial

z = filter(partial(words_to_integer, y=minimumlength), listofwords)


partial(words_to_integer, y=minimumlength)words_to_integer的功能相同,但参数y固定为minimumlength

09-17 17:19