【问题标题】:Use the dimensions of a numpy array as an if-statement python使用 numpy 数组的维度作为 if 语句 python
【发布时间】:2018-06-08 16:05:28
【问题描述】:

我将遍历不同的 numpy 数组。有些是二维的,有些是一维的。如果评估的数组是一维的,我想将它平铺成一个二维数组。 示例:

c = {'a': np.array([1, 2, 3]), 'b' : np.array([[1, 2, 3], [4, 5, 6]])}

for k in c:
    if c[k].shape #is 1D:
        c[k] = np.tile(c[k], (len(c[k]),1))

我不知道如何运行该条件。有任何想法吗? 我尝试过类似

aa = np.array([1, 2, 3])
aa.shape[0]
# 3
aa.shape[1]
# Gives an out of range error

我想可以通过发现没有第二维来找出它是一维数组。但我不知道如何在 if 语句中编写代码。

谢谢

【问题讨论】:

    标签: python if-statement dimensions


    【解决方案1】:

    NumPy 数组有一个名为ndim 的属性,它准确地代表了您认为它的作用:数组的维数。所以,你可以这样做:

    if c[k].ndim == 1:
        # do something
    

    【讨论】:

    • 这很棒而且非常简单!我显然是 python 的新手,虽然我在这里发布之前研究了几个小时,但我没有找到任何东西。谢谢!
    【解决方案2】:

    您需要检查array.shape 有多少元素。因此,您可以通过运行以下命令来检查一维数组

    if len(c[k].shape) == 1
    

    【讨论】:

      【解决方案3】:

      你可以的

      if len(aa.shape)==1:
         # something
      

      【讨论】:

        【解决方案4】:

        dir 一直都在帮助我,尤其是在处理文档不完整的接口时。不像ndarray.

        dir(np.array([]))

        ['T', '__abs__', '__add__', '__and__', '__array__', '__array_finalize__', '__array_interface__', '__array_prepare__', '__array_priority__', '__array_struct__', '__array_wrap__', '__class__', '__complex__', '__contains__', '__copy__', '__deepcopy__', '__delattr__', '__delitem__', '__delslice__', '__div__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__hex__', '__iadd__', '__iand__', '__idiv__', '__ifloordiv__', '__ilshift__', '__imod__', '__imul__', '__index__', '__init__', '__int__', '__invert__', '__ior__', '__ipow__', '__irshift__', '__isub__', '__iter__', '__itruediv__', '__ixor__', '__le__', '__len__', '__long__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__setitem__', '__setslice__', '__setstate__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__xor__', 'all', 'any', 'argmax', 'argmin', 'argpartition', 'argsort', 'astype', 'base', 'byteswap', 'choose', 'clip', 'compress', 'conj', 'conjugate', 'copy', 'ctypes', 'cumprod', 'cumsum', 'data', 'diagonal', 'dot', 'dtype', 'dump', 'dumps', 'fill', 'flags', 'flat', 'flatten', 'getfield', 'imag', 'item', 'itemset', 'itemsize', 'max', 'mean', 'min', 'nbytes', 'ndim', 'newbyteorder', 'nonzero', 'partition', 'prod', 'ptp', 'put', 'ravel', 'real', 'repeat', 'reshape', 'resize', 'round', 'searchsorted', 'setfield', 'setflags', 'shape', 'size', 'sort', 'squeeze', 'std', 'strides', 'sum', 'swapaxes', 'take', 'tobytes', 'tofile', 'tolist', 'tostring', 'trace', 'transpose', 'var', 'view']

        【讨论】:

        • 这也很有用。谢谢!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-29
        • 1970-01-01
        • 2014-08-10
        • 1970-01-01
        • 1970-01-01
        • 2014-11-27
        相关资源
        最近更新 更多