繁体   English   中英

numpy - 将元组或列表插入 numpy 数组项 -

[英]numpy - insert a tuple or a list into numpy array items -

import tables, h5py
import math, sys, time
import numpy as np
import numpy.lib.recfunctions as rf
v_dt = np.dtype([ ('EID','i8'), ('CID','i8'), ('CTYPE','S4'), ('NODEF','i8'), ('f1', '<i8', (5,) )  ])
print (v_dt)
value_list = [ ( 1, 0, 'GRID', 10, (1,2,3,4,5) ) ]
np_value_list = rf.unstructured_to_structured(np.array(value_list), v_dt)
v_rec_arr = np.rec.array(np_value_list, dtype=v_dt)

给我一条错误信息:

VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences 
(which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) 
is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the 
ndarray.
np_value_list = rf.unstructured_to_structured(np.array(value_list), v_dt)
Traceback (most recent call last):
File "digimat_to_hdf5.py", line 13, in <module>
np_value_list = rf.unstructured_to_structured(np.array(value_list), v_dt)
File "<__array_function__ internals>", line 6, in unstructured_to_structured
File "C:\Users\lutz.peschlow\AppData\Roaming\Python\Python37\site-packages\numpy
\lib\recfunctions.py", line 1074, in unstructured_to_structured
raise ValueError('The length of the last dimension of arr must '
ValueError: The length of the last dimension of arr must be equal to the number of fields 
in dtype*

而且我不知道,现在如何设置 np 数组的创建,过去我没有使用元组作为数据集的一部分,

你能帮我吗,我怎样才能为该数据创建一个 numpy 记录数组?

我发现,几个较新的 Python 版本无法像在早期 Python 3.4 版本中那样工作,我感觉,Python 3.7 或 3.9 版本越新,表或 h5py 的问题越多,

上述方法不再适用于较新的版本,作为解决方法,我使用以下方法创建用于编写 hdf5 的数据集:

创建一个单独的数据块预定义:

class INDEX_DISP(IsDescription):
  DOMAIN_ID = Int64Col(pos = 0)
  POSITION = Int64Col(pos = 1)
  LENGTH = Int64Col(pos = 2)

打开 hdf5,赋值并关闭 hdf5:

h5 = open_file('t.h5', mode = 'w')
index_disp = h5.create_table('/INDEX/NODAL','DISP',INDEX_DISP,createparents = True)
row = index_disp.row
for i in range(1,2):
    row['DOMAIN_ID'] = 1
    row['POSITION'] = 0
    row['LENGTH'] = 24
    row.append()
index_disp.flush() 
h5.close()
´´´

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM