简体   繁体   English

列出多个条件

[英]List with multiple conditions

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. 我正在创建一个布尔函数,检查列表中的每个元素是否大于1且小于6.此外,列表是正整数,而不是负数,0,字符串或其他任何内容。

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. 使用生成器表达式构建元素的中间列表,然后针对all元素检查它们以查看它们是否适合您的约束。

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

A single generator expression with all using isinstance(i, int) to check is each element is an int and 1 <= i <= 6 to make sure each int is in the range 1-6: 与单个发电机表达all使用isinstance(i, int)以检查是每个元素是一个int和1 <= i <= 6 ,以确保每个int是在范围1-6:

def checkList(aList):
   return all(isinstance(i, int)  and 1 < i < 6 for i in aList)

all will short circuit and return False for any non int or any value less than 1 or greater the 5 or return True if all the elements are ints and in the range 1-6. all将短路并返回任何非int或任何小于1或大于5的值的False,如果所有元素都是int并且在1-6范围内,则返回True。

In your own code you are checking for if i < 1 and elif i > 6 when you say you want to check if each element in my list is greater than 1 and less than 6 so it would be if i < 2 and elif i > 5 return False, 1 is not less than 1 and 6 is greater than 5, so to correct your own logic and to check for ints. 在您自己的代码中,当您说要检查列表中的每个元素是否大于1且小于 elif i > 6时,您正在检查if i < 1elif i > 6 if i < 2elif i > 5返回False,1不小于1,6大于5,所以要纠正自己的逻辑并检查int。

def checkList(aList):
    for i in aList:
        if not isinstance(i, int):
             return False
        if i < 2:
            return False
        if i > 5:
            return False
    return True # outside loop

You need to move the return True outside loop, just because one is an int between 1 - 6 does not mean they all are. 你需要移动返回True外部循环,因为一个是1到6之间的int并不意味着它们都是。

Which could be rewritten as: 哪个可以改写为:

def checkList(aList):
    for i in aList:
        if not isinstance(i, int):
             return False
        if 2 > i > 5:
            return False              
    return True

This will work for you 这对你有用

def check_sequence(sequence, low, high):
    if all(isinstance(i, int) for i in sequence):
        return all(low < i < high for i in sequence)
    else:
        return False
print(check_sequence([2, 5, 4, 3, 3], 1, 6))
>> True

The important concept here is low < i < high . 这里重要的概念是low < i < high In Python, you can specify a range of values that the variable should be between. 在Python中,您可以指定变量之间应该存在的值范围。

You could just use filter it would go something like this 你可以使用过滤器它会像这样

filtered = filter(seq, lambda x: 1 < int(x) < 6)

That will return a filtered list containing only items which were between 1 and 6. You can then check if the list has the same length to see if anything was removed. 这将返回一个筛选列表,其中仅包含介于1和6之间的项目。然后,您可以检查列表是否具有相同的长度,以查看是否删除了任何内容。

Going this route, your function would look like this: 走这条路,你的功能看起来像这样:

def checkList(seq):
    filtered = filter(seq, lambda x: 1 < int(x) < 6)
    return len(filteted) == len(seq)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM