python - Define dtypes in NumPy using a list? -
I have a problem with NumPy dtypes. Basically I'm trying to create a table that looks like the following (and Then save it using rec2csv):
name1 name2 name3. . Name1 # # # name2 # # # Name 2 # # # .
The matrix (numerical array at the center), the first name tag is calculated before attempting to add. I have tried to use the following code:
dt = dtype ({'names': tuple (blah), 'format': tuple (fmt)}) ReadArray = array (tuplelist , Dtype = dt)
where tuplelist is a list of rows (i.e. the line [name1, #, #, # ...]), blah is a list of strings (i.e. name , blah = ['name1', 'name2', ...]
) and the list of fmt format, s (i.e. fmt = [str, float, float, ...]
).
The error I am getting is the following:
Traceback (the most recent call final): The file "lt; stdin & gt;", line 1, & Lt; In module & gt; The file "table_calc_try2.py", in line 152, in table_calc_try2 dt = dtype ({'names': tuple (blah), 'format': tuple (fmt)}) type type: data type not understood
Can anyone help?
Thanks
The following code can help Np dt = np.dtype ( Tuplelist = [('N1', 1.2), ('N2', 3.4),] arr = [('name1', '| S10'), ('name2', '' S8 ''] Print (arr ['Name2']) # [1.2 3.4]
Your immediate problem was that np.dtype
considers format specifiers as narrow type, such as 'S10'
or '
str
or float
. If you type help (np.dtype)
, you will see several examples of how np.dtypes
can be specified. (I've only mentioned something.)
Note that np.array is expected to have a list of tuples. Instead it is special about
raises a list of lists TypeError: a readable buffer object
.
A (Tuples of Tuples) or one (Tuval of lists) raises Value error: One sequence
. Setting an array element with
Comments
Post a Comment