python: how to identify if a variable is an array or a scalar

后端 未结 14 1337
半阙折子戏
半阙折子戏 2020-12-22 16:13

I have a function that takes the argument NBins. I want to make a call to this function with a scalar 50 or an array [0, 10, 20, 30].

相关标签:
14条回答
  • 2020-12-22 17:13

    Simply use size instead of len!

    >>> from numpy import size
    >>> N = [2, 3, 5]
    >>> size(N)
    3
    >>> N = array([2, 3, 5])
    >>> size(N)
    3
    >>> P = 5
    >>> size(P)
    1
    
    0 讨论(0)
  • 2020-12-22 17:14
    >>> N=[2,3,5]
    >>> P = 5
    >>> type(P)==type(0)
    True
    >>> type([1,2])==type(N)
    True
    >>> type(P)==type([1,2])
    False
    
    0 讨论(0)
提交回复
热议问题