How to use “any” and “all” in Python

Credential: I do not know, but I, at least, am 4 years experienced; I am still learning C and Assembly.

What is any and all in Python?

By Python definition, any will return if there is an item whereby true in a list, whereas all will return if all items are true in a list.

It looks like the operator or and and for any and all. It is actually easy to implement in C, Assembly, or any other language.

Here are examples of the function any,

any([true, false, false]) # Result in True
any([false, false, false]) # Result in False
any([true, false, false])   # Result in True
any([false, false, false])  # Result in False
any([true, false, false]) # Result in True any([false, false, false]) # Result in False

Enter fullscreen mode Exit fullscreen mode

Here are examples of the function all,

all([true, true, true]) # Result in True
all([true, true, false]) # Result in False
all([true, true, true])     # Result in True
all([true, true, false])    # Result in False
all([true, true, true]) # Result in True all([true, true, false]) # Result in False

Enter fullscreen mode Exit fullscreen mode

Implementation in C for any,

// My algorithm for "any"
int any(int* arr, int size) {
int i = 0, t = 0;
for (; i < size - 1 ; i += 2) {
t += arr[i] + arr[i + 1];
}
return (t + arr[size % i]) && 1;
}
// My algorithm for "any"
int any(int* arr, int size) {
    int i = 0, t = 0;
    for (; i < size - 1 ; i += 2) {
        t += arr[i] + arr[i + 1];
    }
    return (t + arr[size % i]) && 1;
}
// My algorithm for "any" int any(int* arr, int size) { int i = 0, t = 0; for (; i < size - 1 ; i += 2) { t += arr[i] + arr[i + 1]; } return (t + arr[size % i]) && 1; }

Enter fullscreen mode Exit fullscreen mode

With a O(log n) time complexity and O(1) space complexity.

Implementation in C for all,

// My algorithm for "all"
int all(int* arr, int size) {
int i = 0, t = 0;
for (; i < size - 1 ; i += 2) {
t += arr[i] + arr[i + 1];
}
return (t + (i % size && arr[size % i]) > (i >> 1));
}
// My algorithm for "all"
int all(int* arr, int size) {
    int i = 0, t = 0;
    for (; i < size - 1 ; i += 2) {
        t += arr[i] + arr[i + 1];
    }
    return (t + (i % size && arr[size % i]) > (i >> 1));
}
// My algorithm for "all" int all(int* arr, int size) { int i = 0, t = 0; for (; i < size - 1 ; i += 2) { t += arr[i] + arr[i + 1]; } return (t + (i % size && arr[size % i]) > (i >> 1)); }

Enter fullscreen mode Exit fullscreen mode

With a O(log n) time complexity and O(1) space complexity.

I will explain how the algorithm works in another post.

How to use any,

# Real world application
if any(person.alive for person in people):
myself.shout("Who is dead?")
# Real world application
if any(person.alive for person in people):
    myself.shout("Who is dead?")
# Real world application if any(person.alive for person in people): myself.shout("Who is dead?")

Enter fullscreen mode Exit fullscreen mode

How to use all,

# Real world application
if all(person.alive for person in people):
myself.shout("We are safe!")
# Real world application
if all(person.alive for person in people):
    myself.shout("We are safe!")
# Real world application if all(person.alive for person in people): myself.shout("We are safe!")

Enter fullscreen mode Exit fullscreen mode

原文链接:How to use “any” and “all” in Python

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享
Like a child, always believe in hope, I believe the dream.
像孩子一样,永远相信希望,相信梦想
评论 抢沙发

请登录后发表评论

    暂无评论内容