python - 使用和条件循环列表
问题描述
我正在尝试为以下伪代码编写代码
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] )
推荐阅读
- node.js - 在 hyper 中安装 npm 包
- java - Kafka Kstream Json Join 无法转换为 java.lang.String
- php - 在 API 平台的实体中定义数字时遇到问题
- python - 如何使用定义为游戏对象的记录来保存多个记录数据,然后将其打印出来
- java - 在插件开发中使用速度
- amazon-web-services - AWS Glue 动态过滤 - 使用另一个动态帧过滤一个动态帧
- php - 为每个结果分配一个数字而不在数据库中添加一列 - Laravel
- java - Flink 反序列化 Kafka JSON
- php - 无法将值从表单传递到 php 页面
- spring - 为什么 Hibernate Search 索引无法正常工作