Python 中带有回调的 any() 函数
Python 标准库定义了一个 any()
函数那
如果可迭代的任何元素为 true,则返回 True。如果迭代为空,则返回 False。
它仅检查元素的计算结果是否为 True
。我希望它能够指定一个回调来判断某个元素是否符合要求,例如:
any([1, 2, 'joe'], lambda e: isinstance(e, int) and e > 0)
The Python standard library defines an any()
function that
Return True if any element of the iterable is true. If the iterable is empty, return False.
It checks only if the elements evaluate to True
. What I want it to be able so specify a callback to tell if an element fits the bill like:
any([1, 2, 'joe'], lambda e: isinstance(e, int) and e > 0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
怎么样:
当然它也可以与
all()
一起使用:How about:
It also works with
all()
of course:当任何条件为 True 时,any 函数返回 True。
实际上,any函数的概念是从Lisp中引入的,或者可以说是从函数编程方法中引入的。还有一个与之相反的函数是all
这两个函数如果使用得当的话真的很酷。
any function returns True when any condition is True.
Actually,the concept of any function is brought from Lisp or you can say from the function programming approach. There is another function which is just opposite to it is all
These two functions are really cool when used properly.
您应该使用“生成器表达式” - 即可以使用迭代器并在一行上应用过滤器和表达式的语言构造:
例如
(i ** 2 for i in xrange(10))< /code> 是前 10 个自然数(0 到 9)的平方的生成器,
它们还允许使用“if”子句来过滤“for”子句上的 itens,因此对于您的示例,您可以使用:
You should use a "generator expression" - that is, a language construct that can consume iterators and apply filter and expressions on then on a single line:
For example
(i ** 2 for i in xrange(10))
is a generator for the square of the first 10 natural numbers (0 to 9)They also allow an "if" clause to filter the itens on the "for" clause, so for your example you can use:
对 Antoine P 的答案
For
all()
略有改进Slight improvement to Antoine P's answer
For
all()
虽然其他人给出了很好的 Pythonic 答案(在大多数情况下我只是使用公认的答案),但我只是想指出,如果你真的喜欢的话,创建自己的实用函数来自己完成此操作是多么容易:
我想我不过,至少首先使用函数参数定义它,因为这会更接近地匹配现有的内置函数,如map()和filter():
While the others gave good Pythonic answers (I'd just use the accepted answer in most cases), I just wanted to point out how easy it is to make your own utility function to do this yourself if you really prefer it:
I think I'd at least define it with the function parameter first though, since that'd more closely match existing built-in functions like map() and filter():
如果您确实想像这样保留 lambda 表示法,则可以使用
any
和map
的组合:但最好使用生成器表达式,因为它不会构建整个列表两次。
You can use a combination of
any
andmap
if you really want to keep your lambda notation like so :But it is better to use a generator expression because it will not build the whole list twice.
过滤器可以工作,而且它会返回匹配的元素
filter can work, plus it returns you the matching elements
如果您确实想在any()中内联一个lambda,您可以这样做:
您只需包装未命名的lambda并确保在每次传递时通过附加
()<来调用它/code>
这里的优点是,当你击中第一个 int 时,你仍然可以利用短路 any 的评估
If you really want to inline a lambda in any() you can do this:
You just have to wrap up the unnamed lambda and ensure it is invoked on each pass by appending the
()
The advantage here is that you still get to take advantage of short circuiting the evaluation of any when you hit the first int