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.
我不知道哪里出错了? 有功能代码&#34; make2d&#34; :
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))['_']
答案 0 :(得分:0)
这段代码来自哪里?在dtype
中使用复合fromiter
非常棘手。
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,))])