cta.19 2021-11-25 14:22 采纳率: 100%
浏览 25
已结题

如何用Python输出numpy中的所有函数和方法

代码越简单越好
输出三个列表:
lsf为仅是函数不是方法的那些
lsm为仅是方法不是函数的那些
lsfm为既是函数又是方法的那些

  • 写回答

1条回答 默认 最新

  • 天元浪子 Python领域优质创作者 2021-11-25 15:03
    关注

    函数和方法是如何定义的呢?感觉题主说的是不同命名空间的问题吧?比如,一个ndarray对象有min()方法,但是在np的顶级命名空间中也存在np.min()函数。楼主是想区分这两类吗?实际上,np顶级命名空间有超过600个类或函数,每个类下又有少则几十多则上百的方法,把它们全部显示出来没有多少意义。如果题主一定要这样做,可以试试下面的方法。

    先看看np顶级命名空间下的(太多了,仅显示前8个)

    >>> for item in dir(np)[:8]:
        print(item)
    
        
    ALLOW_THREADS
    AxisError
    BUFSIZE
    Bytes0
    CLIP
    ComplexWarning
    DataSource
    Datetime64
    >>> 
    

    稍微改造一下这段代码,也可以只显式可以被调用的函数或方法。以np.ndarray对象为例。

    >>> a = np.array([])
    >>> for item in dir(a):
        if hasattr(a.__getattribute__(item), '__call__'):
            print(item)
    
            
    __abs__
    __add__
    __and__
    __array__
    __array_function__
    __array_prepare__
    __array_ufunc__
    __array_wrap__
    __bool__
    __class__
    __complex__
    __contains__
    __copy__
    __deepcopy__
    __delattr__
    __delitem__
    __dir__
    __divmod__
    __eq__
    __float__
    __floordiv__
    __format__
    __ge__
    __getattribute__
    __getitem__
    __gt__
    __iadd__
    __iand__
    __ifloordiv__
    __ilshift__
    __imatmul__
    __imod__
    __imul__
    __index__
    __init__
    __init_subclass__
    __int__
    __invert__
    __ior__
    __ipow__
    __irshift__
    __isub__
    __iter__
    __itruediv__
    __ixor__
    __le__
    __len__
    __lshift__
    __lt__
    __matmul__
    __mod__
    __mul__
    __ne__
    __neg__
    __new__
    __or__
    __pos__
    __pow__
    __radd__
    __rand__
    __rdivmod__
    __reduce__
    __reduce_ex__
    __repr__
    __rfloordiv__
    __rlshift__
    __rmatmul__
    __rmod__
    __rmul__
    __ror__
    __rpow__
    __rrshift__
    __rshift__
    __rsub__
    __rtruediv__
    __rxor__
    __setattr__
    __setitem__
    __setstate__
    __sizeof__
    __str__
    __sub__
    __subclasshook__
    __truediv__
    __xor__
    all
    any
    argmax
    argmin
    argpartition
    argsort
    astype
    byteswap
    choose
    clip
    compress
    conj
    conjugate
    copy
    cumprod
    cumsum
    diagonal
    dot
    dump
    dumps
    fill
    flatten
    getfield
    item
    itemset
    max
    mean
    min
    newbyteorder
    nonzero
    partition
    prod
    ptp
    put
    ravel
    repeat
    reshape
    resize
    round
    searchsorted
    setfield
    setflags
    sort
    squeeze
    std
    sum
    swapaxes
    take
    tobytes
    tofile
    tolist
    tostring
    trace
    transpose
    var
    view
    >>> 
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月3日
  • 已采纳回答 11月25日
  • 创建了问题 11月25日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来