1
    if triangles is None:
        tridata = mesh['face'].data['vertex_indices']
        print(tridata)
        print(type(tridata))
        print(tridata.dtype)
        triangles = plyfile.make2d(tridata)

有一个错误:用一个序列设置一个数组元素。我检查 tridata 的类型:

[array([    0,  5196, 10100], dtype=int32)
 array([    0,  2850, 10103], dtype=int32)
 array([    0,  3112, 10102], dtype=int32) ...
 array([ 2849, 10076,  5728], dtype=int32)
 array([ 2849, 10099,  8465], dtype=int32)
 array([ 2849, 10098,  8602], dtype=int32)]

<class 'numpy.ndarray'>

object
ValueError:Error:setting an array element with a sequence.

不知道哪里错了?有函数“make2d”的代码:

def make2d(array, cols=None, dtype=None):
    '''
    Make a 2D array from an array of arrays.  The `cols' and `dtype'
    arguments can be omitted if the array is not empty.

    '''
    if (cols is None or dtype is None) and not len(array):
        raise RuntimeError("cols and dtype must be specified for empty "
                           "array")

    if cols is None:
        cols = len(array[0])

    if dtype is None:
        dtype = array[0].dtype

    return _np.fromiter(array, [('_', dtype, (cols,))],
                        count=len(array))['_']
4

1 回答 1

0

这段代码来自哪里?化合物的使用dtypefromiter棘手。

In [102]: dt1=np.dtype([('_',int,(4,))])
In [103]: dt2=np.dtype('i,i,i,i')
In [104]: x = np.arange(12).reshape(3,4)
In [105]: np.fromiter(x, dt1)
....
ValueError: setting an array element with a sequence.
In [106]: np.fromiter(x, dt2)
...
ValueError: setting an array element with a sequence.

如果我展平数组,它可以工作 - 除了值被复制:

In [107]: np.fromiter(x.ravel(), dt1)
Out[107]: 
array([([ 0,  0,  0,  0],), ([ 1,  1,  1,  1],), ([ 2,  2,  2,  2],),
       ([ 3,  3,  3,  3],), ([ 4,  4,  4,  4],), ([ 5,  5,  5,  5],),
       ([ 6,  6,  6,  6],), ([ 7,  7,  7,  7],), ([ 8,  8,  8,  8],),
       ([ 9,  9,  9,  9],), ([10, 10, 10, 10],), ([11, 11, 11, 11],)],
      dtype=[('_', '<i8', (4,))])

将数组转换为嵌套列表,可以:

In [108]: np.fromiter(x.tolist(), dt1)
Out[108]: 
array([([ 0,  1,  2,  3],), ([ 4,  5,  6,  7],), ([ 8,  9, 10, 11],)],
      dtype=[('_', '<i8', (4,))])
In [109]: np.fromiter(x.tolist(), dt2)
....
ValueError: setting an array element with a sequence.

但是如果我把它做成一个元组列表,我就可以创建这个结构化数组。元组列表是填充结构化数组的常规方式。

In [110]: np.fromiter([tuple(i) for i in x.tolist()], dt2)
Out[110]: 
array([(0, 1,  2,  3), (4, 5,  6,  7), (8, 9, 10, 11)],
      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4'), ('f3', '<i4')])

但是对于对象 dtype 数组,这些技巧都不起作用:

In [111]: a
Out[111]: 
array([array([0, 1, 2, 3]), array([5, 6, 7, 8]), array([10, 11, 12, 13])],
      dtype=object)

我可以dt1使用对初始化数组的赋值来创建一个数组:

In [123]: b = np.zeros((3,), dt1)
In [124]: b
Out[124]: 
array([([0, 0, 0, 0],), ([0, 0, 0, 0],), ([0, 0, 0, 0],)],
      dtype=[('_', '<i8', (4,))])
In [125]: b['_']=x
In [126]: b
Out[126]: 
array([([ 0,  1,  2,  3],), ([ 4,  5,  6,  7],), ([ 8,  9, 10, 11],)],
      dtype=[('_', '<i8', (4,))])

我也可以从数组数组中迭代地填充它:

In [128]: for i in range(3):
     ...:     b['_'][i]=a[i]
     ...:     
In [129]: b
Out[129]: 
array([([ 0,  1,  2,  3],), ([ 5,  6,  7,  8],), ([10, 11, 12, 13],)],
      dtype=[('_', '<i8', (4,))])
于 2018-03-07T17:27:01.643 回答