Python代码 在线执行

Python 程序:查找可被另一个数整除的数字

/examples/number-divisible

在此程序中,您将学习找到可被另一个数字整除的数字并显示它。

要理解此示例,您应该了解以下 [Python 编程]( "Python tutorial")主题:


在下面的程序中,我们使用了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]