python中any()和all()如何使用

python中any()和all()如何使用

any(iterables)all(iterables)对于检查两个对象相等时非常实用,但是要注意,anyall是python内置函数,同时numpy也有自己实现的anyall,功能与python内置的一样,只不过把numpy.ndarray类型加进去了。因为python内置的对高于1维的ndarray没法理解,所以numpy基于的计算最好用numpy自己实现的anyall

本质上讲,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, return False.

伪代码(其实是可以运行的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 os
import inspect as inspect
import pandas as pd

path = 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
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值