Python np.array的len()给出了TypeError:未大小对象的len()

Python np.array的len()给出了TypeError:未大小对象的len(),python,arrays,python-3.x,python-2.7,numpy,Python,Arrays,Python 3.x,Python 2.7,Numpy,我需要将一个相当大的Python2.7项目更新为Python3。免责声明,我是python新手,这是一项让我学习python语言的任务。棘手的部分如下: assert ((nzis is None and shape is not None) or (nzis is not None and shape is None)) # Set non-zero indices of the object mask's if nzis is None:

我需要将一个相当大的Python2.7项目更新为Python3。免责声明,我是python新手,这是一项让我学习python语言的任务。棘手的部分如下:

assert ((nzis is None and shape is not None) or
            (nzis is not None and shape is None))

    # Set non-zero indices of the object mask's
    if nzis is None:
        self._nzis = shape_to_nzis(shape)
    else:
        self._nzis = np.array(nzis)
稍后,将调用以下命令:

assert len(self._nzis) <= MAX_NZIS_PER_ENTITY

assert len(self.\nzis)搜索我找到的网页


看看。什么是形状?一些在Py2中生成列表的函数在Py3中生成类似生成器的对象。例如,Py3中的
range(3)
更像Py2中的
xrange(3)
。您必须使用
list(…)
展开此类对象,才能执行
len()
之类的操作。
def shape_to_nzis(shape):
    """
    Convert a shape tuple (int, int) to NZIs.
    """
    return np.array(zip(*np.ones(shape).nonzero()))

In [48]: np.array(zip(*np.ones((3,4)).nonzero()))                               
Out[48]: array(<zip object at 0x7f39a009afc8>, dtype=object)
In [49]: len(_)                                                                 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-49-556fcc1c5d2a> in <module>
----> 1 len(_)

TypeError: len() of unsized object
In [50]: np.array(list(zip(*np.ones((3,4)).nonzero())))                         
Out[50]: 
array([[0, 0],
       [0, 1],
       [0, 2],
       [0, 3],
       [1, 0],
       [1, 1],
       [1, 2],
       [1, 3],
       [2, 0],
       [2, 1],
       [2, 2],
       [2, 3]])