Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将字符串列表转换为预设数据类型的numpy数组_Python_Arrays_Numpy - Fatal编程技术网

Python 将字符串列表转换为预设数据类型的numpy数组

Python 将字符串列表转换为预设数据类型的numpy数组,python,arrays,numpy,Python,Arrays,Numpy,我有一个字符串列表string_list,我想将其转换为预先指定数据类型的1元素结构化numpy数组: string_list = ['123', '45', '6.7'] my_dtype = np.dtype([('a','i4'),('b','f4'),('c','f8')]) 我想要的输出结果是: array([(123, 45., 6.7)], dtype=[('a', '<i4'), ('b', '<f4'), ('c', '<f8')]) array([(12

我有一个字符串列表string_list,我想将其转换为预先指定数据类型的1元素结构化numpy数组:

string_list = ['123', '45', '6.7']
my_dtype = np.dtype([('a','i4'),('b','f4'),('c','f8')])
我想要的输出结果是:

array([(123, 45., 6.7)], dtype=[('a', '<i4'), ('b', '<f4'), ('c', '<f8')])

array([(123,45,6.7)],dtype=[('a','p>结构数组的'rows'的输入是元组:

In [464]: np.array([tuple(string_list)],dtype=my_dtype)
Out[464]: 
array([(123, 45.0, 6.7)], 
      dtype=[('a', '<i4'), ('b', '<f4'), ('c', '<f8')])
[464]中的
:np.array([tuple(string\u list)],dtype=my\u dtype)
出[464]:
数组([(123,45.0,6.7)],

dtype=[('a','编辑请参见@hpaulj answer。列表必须转换为元组,列表生成错误

对我来说这很有效,这就是你想要的吗

string_list = ('123', '45.', '6.7')
b = np.array([string_list], dtype=[('a', '<i4'), ('b', '<f4'), ('c', '<f8')])
string_list=('123','45','6.7')

b=np.array([string_list],dtype=[('a','是的,元组转换就是我所缺少的。谢谢!