在下面的程序中,我们在 filter() 内置函数中使用了匿名(lambda)函数,以找出列表中所有能被 13 整除的数字。
源代码
# Take a list of numbers
my_list = [12, 65, 54, 39, 102, 339, 221,]
# use anonymous function to filter
result = list(filter(lambda x: (x % 13 == 0), my_list))
# display the result
print("Numbers divisible by 13 are",result)
输出
Numbers divisible by 13 are [65, 39, 221]
另请阅读