any()函数在Python中用回调 [英] any() function in Python with a callback

查看:204
本文介绍了any()函数在Python中用回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python标准库定义了 any() 函数

The Python standard library defines an any() function that


如果iterable的任何元素为true,则返回True。如果iterable为空,返回False。

Return True if any element of the iterable is true. If the iterable is empty, return False.

它只检查元素是否为 True 。我想让它能够这样指定一个回调来告诉一个元素是否符合账单:

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)


推荐答案

如何:

>>> any(isinstance(e, int) and e > 0 for e in [1,2,'joe'])
True

它也可以用于 all()当然:

>>> all(isinstance(e, int) and e > 0 for e in [1,2,'joe'])
False

这篇关于any()函数在Python中用回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆