Python 程序,用于打印由列表中给定元素的倍数的元素
当需要打印由列表中给定元素的倍数的元素时,使用列表解析。
示例
以下是相同的演示
my_list = [45, 67, 89, 90, 10, 98, 10, 12, 23] print("The list is :") print(my_list) my_division_list = [6, 4] print("The division list is :") print(my_division_list) my_result = [element for element in my_list if all(element % j == 0 for j in my_division_list)] print("The result is :") print(my_result)
输出
The list is : [45, 67, 89, 90, 10, 98, 10, 12, 23] The division list is : [6, 4] The result is : [12]
Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.
解释
定义一个列表并将其显示在控制台上。
定义了另一个整数列表。
使用列表解析遍历元素并检查元素除以整数列表中的元素是否余数为 0。
如果是,则将其存储在列表中并分配给变量。
这将作为输出显示在控制台上。
广告