首页 > 解决方案 > 使用和条件循环列表

问题描述

我正在尝试为以下伪代码编写代码

for all element in list do
    match and condition
    if all match
       return True

例如,列表 A=[1,2,3,4,5],B=10

我想要的就像

def match():
    for i in range(len(A)):
      if B%A[0]==0 and B%A[1]==0 and B%A[2]==0 and B%A[3]==0 and B%A[4]==0: #Generate all these 
        #and condition one by one
        #automatically in this function
          return True

我能怎么做?

注意:我问的是用循环编写代码匹配和条件,而不是写余数

标签: pythonalgorithm

解决方案


试试这个:

result = all( [(B%a==0) for a in A] )

推荐阅读