python中any()和all()如何使用
来源:互联网 发布:js监听scroll事件 编辑:程序博客网 时间:2024/06/04 18:29
python中any()和all()如何使用
any(iterables)
和all(iterables)
对于检查两个对象相等时非常实用,但是要注意,any
和all
是python内置函数,同时numpy也有自己实现的any
和all
,功能与python内置的一样,只不过把numpy.ndarray
类型加进去了。因为python内置的对高于1维的ndarray
没法理解,所以numpy基于的计算最好用numpy自己实现的any
和all
。
本质上讲,any()
实现了或(OR)运算,而all()
实现了与(AND)运算。
对于any(iterables),如果可迭代对象iterables(至于什么是可迭代对象,可关注我的下篇文章)中任意存在每一个元素为True
则返回True
。特例:若可迭代对象为空,比如空列表[]
,则返回False
。
官方文档如是说:
Return
True
if any element of the iterable is true. If the iterable is empty, returnFalse
.
伪代码(其实是可以运行的python代码,但内置的any是由C写的)实现方式:
def any(iterable): for element in iterable: if element: return True return False
对于all(iterables),如果可迭代对象iterables中所有元素都为True
则返回True
。特例:若可迭代对象为空,比如空列表[]
,则返回True
。
官方文档如是说:
Return
True
if all elements of the iterable are true (or if the iterable is empty).
伪代码(其实是可以运行的python代码,但内置的all是由C写的)实现方式:
def all(iterable): for element in iterable: if not element: return False return True
python的模块由两类语言开发,一类为纯python,一类为编译型语言,比如C/C++/Fortran。绝大多数标准库由纯python开发,这是由于python语言具有简洁性及短的开发周期。这些模块的源码很容易获得,在ipython下可用module??
打印到屏幕上查看,或者写一个脚本保存起来,比如下面这样:
import osimport inspect as inspectimport pandas as pdpath = r"D:\python3_dev\package_down"os.chdir(path)series = inspect.getsource(pd.Series)with open("pd_series_code.py", "w") as f: f.write(series)
当然,也可以到python安装包下查找,但是效率很低。
python inspect.getsource(object)
只能查看用python写的module, class, method, function, traceback, frame, or code object
。可以看看getsource的文档字符串,了解其基本功能。
>>>inspect.getsource.__doc__'Return the text of the source code for an object.\n\n The argument may be a module, class, method, function, traceback, frame,\n or code object. The source code is returned as a single string. An\n OSError is raised if the source code cannot be retrieved.'
对于有些模块,通常是关乎运行性能的,一般都由编译型语言开发,比如os
模块和for循环N多层的线性代数等模块。所以无法通过getsource
方法获得源码,通常会抛出一个TypeError
异常。要查看这些模块的源码,需要到GitHub上的python/cpython
目录下找,比如在Objects目录下可找到listobject.c
,这是list
模块的C代码。
那么怎么知道一个模块是内置的呢?可用type(object)
或object.__module__
。比如
>>>type(all)builtin_function_or_method
>>>all.__module__'builtins'
一个2X3 ndarray 的例子。用numpy自己实现的all
很容易判断两个array是否相等,但python内置的却抛出了异常。
>>>x = np.ones((2,3))>>>x1 = np.ones((2,3))>>>np.all(x==x1)True>>>xe = x==x1>>>xe.all()True>>>all(xe)#这里调用了python内置模块all()ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
但是,还有更pythonic的写法,因为numpy有一个模块array_equal
,可以直接判断两个array是否完全相等或某个轴相等,其实他也是用np.all
实现的。
>>>np.array_equal(x, x1)True
其实也不要惊讶,这只是python的常规操作。轮子已经被匠人造好,拿来用就OK了!如果你觉得自己可以造出更金光闪闪的轮子,那就抡起你的斧头;如果觉得已经够完美,那就拿来主义,不要再造了。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(内容同步更新到微信公众号python数学物理,微信号python_math_physics)
- python中any()和all()如何使用
- python中all()和any()的区别
- python any和all
- python any()和all()用法
- python any()和all()用法
- python any()和all()用法
- [Python]Build-in方法all和any
- python中的函数any()和all()
- python中的函数any()和all()
- python内建函数any()和all()
- oracle中all,any的区别使用
- matlab 中all和any函数
- matlab中all和any用法
- 数据库中Any和All区别
- python: all & any 函数
- 在oracle的子查询中使用some,any和all
- 在oracle的子查询中使用some,any和all
- 在oracle的子查询中使用some,any和all
- 链表问题——复制含有随机指针节点的链表
- ssh框架整合的步骤
- android子进程发送toast
- openstack4j 源码分析(二) model对象
- MYSQL.导入超过100M数据库方法,MYSQL命令行导入大数据库方法
- python中any()和all()如何使用
- Hadoop简介
- 图像处理基础(6):锐化空间滤波器
- Ceph+RGW部署方案
- 7-41 高空坠球(20 分)
- C++枚举使用
- 简单的商品列表展示
- base64解码 python实现
- Python与医疗图像6