此输入如何与Python一起使用and'功能?
def any(iterable):
for element in iterable:
if element:
return True
return False
此函数如何知道我想测试的元素,如果以此形式调用它?
any(x > 0 for x in list)
从函数定义来看,我只能看到我通过一个可观的对象。 循环的如何知道我正在寻找
> 0
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您使用
任何(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)
andfalse(s)
,值将为作为生成的任何
函数都会通过发电机表达式一次生成的值迭代。 和,由于任何
短路,它将在看到第一个true
值后立即停止迭代。如果您使用lst = range(-1,int(1e9))
(或xrange
),如果您创建lst
,这将特别方便正在使用 python2.x )。即使此表达式将生成十亿个条目,在到达1
时,任何都只需要走第三个条目,该条目评估true> true
代码>用于x> 0
,因此任何
都可以返回true
。如果您创建了一个 list理解,则Python首先必须在内存中创建十亿个元素列表,然后将其传递给
任何
。但是,通过使用 Generator表达式,您可以拥有Python的内置功能,例如任何
andall
尽早爆发,true
或false
值可见。If you use
any(lst)
you see thatlst
is the iterable, which is a list of some items. If it contained[0, False, '', 0.0, [], {}, None]
(which all have boolean values ofFalse
) thenany(lst)
would beFalse
. Iflst
also contained any of the following[-1, True, "X", 0.00001]
(all of which evaluate toTrue
) thenany(lst)
would beTrue
.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 thelst
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 theany
function, which would returnTrue
, since there is at least oneTrue
value.But with generator expressions, Python no longer has to create that internal list of
True(s)
andFalse(s)
, the values will be generated as theany
function iterates through the values generated one at a time by the generator expression. And, sinceany
short-circuits, it will stop iterating as soon as it sees the firstTrue
value. This would be especially handy if you createdlst
using something likelst = range(-1,int(1e9))
(orxrange
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 to1
, which evaluatesTrue
forx>0
, and soany
can returnTrue
.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 likeany
andall
break out early, as soon as aTrue
orFalse
value is seen.它只是将几行代码减少为一条。
您不必写冗长的代码:
It just reduce several line of code into one.
You don't have to write lengthy code like:
(x> 0 for List中的x)
在该功能调用中创建一个发电机表达式,例如。哪个
任何
使用,以及遇到第一个评估true
的对象时的快速电路(x > 0 for x in list)
in that function call creates a generator expression eg.Which
any
uses, and shortcircuits on encountering the first object that evaluatesTrue
这是因为峰值是
x> 0
返回true
或false
,因此您有一个布尔值。It's because the iterable is
Note that
x > 0
returns eitherTrue
orFalse
and thus you have an iterable of booleans.简单地说,任何()可以做到这一点:根据条件即使遇到一个满足列表中的值,它也会返回true,否则返回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.