Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带和条件的循环列表_Python_Algorithm - Fatal编程技术网

Python 带和条件的循环列表

Python 带和条件的循环列表,python,algorithm,Python,Algorithm,我正在尝试为下面的伪代码编写代码 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

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

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
我该怎么办

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

result = all( [(B%a==0) for a in A] )
试试这个:

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

你可以使用一个蟒蛇式的单衬里

result=all(B%x==0表示A中的x)

或者用更熟悉的语法

res = True
for x in A:
    if B % x != 0:
        res = False
        break

你可以使用一个蟒蛇式的单衬里

result=all(B%x==0表示A中的x)

或者用更熟悉的语法

res = True
for x in A:
    if B % x != 0:
        res = False
        break

您可以删除
[]
结果=all(B%x==0表示A中的x)
。您可以节省一些内存,因为这不会生成list@MitchelPaulin我特意用这种方式写它是为了让它更容易理解,因为添加
[]
会让它更容易理解吗?@mitchelpulin我希望它会。最初的问题与使用额外内存或使用最小字符量无关,更像是理解或基本结构以及可能的操作。您可以删除
[]
result=all(B%x==0表示A中的x)
。您可以节省一些内存,因为这不会生成list@MitchelPaulin我特意用这种方式写它是为了让它更容易理解,因为添加
[]
会让它更容易理解吗?@mitchelpulin我希望它会。最初的问题与使用额外内存或使用最小字符量无关,更像是理解或基本结构以及可能的操作。这是一个很好的介绍。请查看python
all
函数。这里有一个很好的介绍。