检查列表中的所有值是否都大于特定数字x且小于y?

时间:2019-01-11 10:11:50

标签: python

public function webhook() 
{
    $webhook_endpoint_secret = env("GOCARDLESS_WEBHOOK_ENDPOINT_SECRET");
    $request_body = file_get_contents('php://input');

    $headers = getallheaders();
    $signature_header = $headers["Webhook-Signature"];

    try {
        $events = Webhook::parse($request_body, $signature_header, $webhook_endpoint_secret);

        foreach ($events as $event) {
            print("Processing event " . $event->id . "\n");

            switch ($event->resource_type) {
                case "mandates":
                    $this->process_mandate_event($event);
                    break;
                default:
                    print("Don't know how to process an event with resource_type " . $event->resource_type . "\n");
                    break;
            }
        }

        header("HTTP/1.1 204 OK");
    } catch(InvalidSignatureException $e) {
        header("HTTP/1.1 498 Invalid Token");
    }
}


public function process_mandate_event($event)
{
    switch ($event->action) {
        case "cancelled":
            print("Mandate " . $event->links["mandate"] . " has been cancelled!\n");
            break;
        default:
            print("Don't know how to process a mandate " . $event->action . " event\n");
            break;
    }
}

如何检查列表中的值是否大于def fun_lst(lst, a, b): if min(lst)<b and max(lst)>a: return True return False 但小于a?我尝试了上面的方法,但在此示例中:b该函数返回fun_lst([-1, 3.5, 6], -2.4, 0),它应该返回True

7 个答案:

答案 0 :(得分:3)

执行以下操作:

def foo
rescue
end

输出:

def fun_lst(lst, a, b):
    if min(lst) > a and max(lst) < b:
        return True
    return False

print(fun_lst([-1, 3.5, 6], -2.4, 0) )                              
  • 进行False 可以确保每个元素比一个元素更大
  • 进行min(lst) > a可以确保每个元素比b

替代解决方案:

max(lst) < b

答案 1 :(得分:3)

如果愿意的话,您可以尝试这款内胆

all([num < b and num > a for num in lst])

答案 2 :(得分:1)

此处的代码将检查列表中的每个项目,如果找到一个不大于a且小于b的项目,则返回false,否则返回true。

def fun_lst(lst, a, b):
    for item in lst:
        if not a < item < b:
            return False
    return True

答案 3 :(得分:0)

myList = [1,2,3,4,5,6,7,8,9]
lower_limit = 3
upper_limit = 8
bool_output = all([i > lower_limit and i < upper_limit for i in myList])
print(bool_output)
    False

myList = [1,2,3,4,5,6,7,8,9]
lower_limit = 0
upper_limit = 10
bool_output = all([i > lower_limit and i < upper_limit for i in myList])
print(bool_output)
    True

答案 4 :(得分:0)

您应该尝试以下操作:

def fun_lst(lst, a, b):
    return all(n > a and n < b for n in lst)

答案 5 :(得分:0)

如果您准备使用numpy,请尝试

In [1]: import numpy as np

In [3]: np.array(b)
Out[3]: array([ 3,  1,  4, 66,  8,  3,  4, 56])

In [17]: b[(2<b) & (b<5)]
Out[17]: array([3, 4, 3, 4])

答案 6 :(得分:0)

不同的方法:

def fun_lst(lis, x, y):
    list = [i>x and i<y for i in lis]
    return False if False in list else True

这很简单:

def fun_lst(lis, x, y):
    return x<max(lis)<y

lambda版本:

fun_lst = lambda lis, x, y: x<max(lis)<y

输出:

fun_lst([-1, 3.5, 6], -2.4, 0) #Output: False