列出多个条件 [英] List with multiple conditions

查看:163
本文介绍了列出多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个布尔函数来检查列表中的每个元素是否大于1且小于6.此外,列表是正整数,而不是负数,0,字符串或其他任何东西。 b

我觉得我已经尝试了几乎所有的东西,找不到我的解决方案。

  def checkList(aList):

for i in aList:
if i< 1:
return False
elif i> 6:
返回False
else:
return True


解决方案使用生成器表达式来构建一个元素的中间列表,然后检查它们是否符合你的约束 all (b)

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ def $ check $ $ $ 6为我在li))


I am creating a boolean function that checks if each element in my list is greater than 1 and less than 6. Also the list is positive integers and not negative, 0, string or anything else.

I feel like I have tried almost everything and cant find my solution. This is what I tentatively have right now.

def checkList(aList):

    for i in aList:
        if i < 1:
            return False
        elif i > 6:
            return False
        else:
            return True

解决方案

Use a generator expression to build an intermediate list of elements, then check them against all to see if they fit in your constraints.

def check_list(li):
    return all((type(i) == int and 1 < i < 6 for i in li))

这篇关于列出多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆