此输入如何与Python一起使用and'功能?

发布于 2025-01-25 02:29:34 字数 433 浏览 0 评论 0 原文

任何()函数的函数为:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

此函数如何知道我想测试的元素,如果以此形式调用它?

any(x > 0 for x in list)

从函数定义来看,我只能看到我通过一个可观的对象。 循环的如何知道我正在寻找> 0

In the python docs page for any, the equivalent code for the any() function is given as:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

How does this function know what element I wanna test if call it in this form?

any(x > 0 for x in list)

From the function definition, all I can see is that I'm passing an iterable object. How does the for loop know I am looking for something > 0?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光匆匆的小流年 2025-02-01 02:29:34

如果您使用任何(LST)您会发现 lst 是迭代的,这是某些项目的列表。如果它包含 [0,false,'',0.0,[],{},none] (它们都具有 false> false )的布尔值,则任何( lst)将是 false 。如果 lst 还包含以下任何一个 [-1,trie,“ x”,0.00001] (所有这些评估为 true )然后任何(LST)将是 true

在您发布的代码中, x> 0对于x中的x ,这是另一种迭代,称为 Generator表达式。在将生成器表达式添加到Python之前,您将创建一个 list consension ,看起来非常相似,但是周围 [] 's: [x> x在lst] 中的0。从 lst 包含 [ - 1,-2,10,-4,20] ,您将获得此理解列表 [false,false,true,false,true] 。然后,此内部值将传递给任何函数,该功能将返回 true ,因为至少有一个 true 值。

但是使用发电机表达式,Python不再需要创建该内部列表 true(s) and false(s),值将为作为生成的任何函数都会通过发电机表达式一次生成的值迭代。 ,由于任何短路,它将在看到第一个 true 值后立即停止迭代。如果您使用 lst = range(-1,int(1e9))(或 xrange ),如果您创建 lst ,这将特别方便正在使用 python2.x )。即使此表达式将生成十亿个条目,在到达 1 时,任何都只需要走第三个条目,该条目评估 true> true 代码>用于 x> 0 ,因此任何都可以返回 true

如果您创建了一个 list理解,则Python首先必须在内存中创建十亿个元素列表,然后将其传递给任何。但是,通过使用 Generator表达式,您可以拥有Python的内置功能,例如任何 and all 尽早爆发, true false 值可见。

If you use any(lst) you see that lst is the iterable, which is a list of some items. If it contained [0, False, '', 0.0, [], {}, None] (which all have boolean values of False) then any(lst) would be False. If lst also contained any of the following [-1, True, "X", 0.00001] (all of which evaluate to True) then any(lst) would be True.

In the code you posted, x > 0 for x in lst, this is a different kind of iterable, called a generator expression. Before generator expressions were added to Python, you would have created a list comprehension, which looks very similar, but with surrounding []'s: [x > 0 for x in lst]. From the lst containing [-1, -2, 10, -4, 20], you would get this comprehended list: [False, False, True, False, True]. This internal value would then get passed to the any function, which would return True, since there is at least one True value.

But with generator expressions, Python no longer has to create that internal list of True(s) and False(s), the values will be generated as the any function iterates through the values generated one at a time by the generator expression. And, since any short-circuits, it will stop iterating as soon as it sees the first True value. This would be especially handy if you created lst using something like lst = range(-1,int(1e9)) (or xrange if you are using Python2.x). Even though this expression will generate over a billion entries, any only has to go as far as the third entry when it gets to 1, which evaluates True for x>0, and so any can return True.

If you had created a list comprehension, Python would first have had to create the billion-element list in memory, and then pass that to any. But by using a generator expression, you can have Python's builtin functions like any and all break out early, as soon as a True or False value is seen.

我ぃ本無心為│何有愛 2025-02-01 02:29:34
>>> names = ['King', 'Queen', 'Joker']
>>> any(n in 'King and john' for n in names)
True

>>> all(n in 'King and Queen' for n in names)
False

它只是将几行代码减少为一条。
您不必写冗长的代码:

for n in names:
    if n in 'King and john':
       print True
    else:
       print False
>>> names = ['King', 'Queen', 'Joker']
>>> any(n in 'King and john' for n in names)
True

>>> all(n in 'King and Queen' for n in names)
False

It just reduce several line of code into one.
You don't have to write lengthy code like:

for n in names:
    if n in 'King and john':
       print True
    else:
       print False
不乱于心 2025-02-01 02:29:34

(x> 0 for List中的x)在该功能调用中创建一个发电机表达式,例如。

>>> nums = [1, 2, -1, 9, -5]
>>> genexp = (x > 0 for x in nums)
>>> for x in genexp:
        print x


True
True
False
True
False

哪个任何使用,以及遇到第一个评估 true 的对象时的快速电路

(x > 0 for x in list) in that function call creates a generator expression eg.

>>> nums = [1, 2, -1, 9, -5]
>>> genexp = (x > 0 for x in nums)
>>> for x in genexp:
        print x


True
True
False
True
False

Which any uses, and shortcircuits on encountering the first object that evaluates True

此刻的回忆 2025-02-01 02:29:34

这是因为峰值是

(x > 0 for x in list)

x> 0 返回 true false ,因此您有一个布尔值。

It's because the iterable is

(x > 0 for x in list)

Note that x > 0 returns either True or False and thus you have an iterable of booleans.

悍妇囚夫 2025-02-01 02:29:34

简单地说,任何()可以做到这一点:根据条件即使遇到一个满足列表中的值,它也会返回true,否则返回false。

list = [2,-3,-4,5,6]

a = any(x>0 for x in lst)

print a:
True


list = [2,3,4,5,6,7]

a = any(x<0 for x in lst)

print a:
False

Simply saying, any() does this work : according to the condition even if it encounters one fulfilling value in the list, it returns true, else it returns false.

list = [2,-3,-4,5,6]

a = any(x>0 for x in lst)

print a:
True


list = [2,3,4,5,6,7]

a = any(x<0 for x in lst)

print a:
False
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文