【发布时间】:2015-08-26 03:40:05
【问题描述】:
这类似于How to convert an array of strings to an array of floats in numpy。
我有一个字符串列表:
dat = [
' 1 2 1.040000e+005 0.030000\n',
' 2 7 0.000000e+000 0.030000\n',
' 3 15 0.000000e+000 0.030000\n',
]
这是我创建 numpy 记录数组的失败尝试:
import numpy as np
dat_dtype = [
('I', 'i'),
('J', 'i'),
('val1', 'd'),
('val2', 'd'),
]
# Attempt 1
np.array(dat, dat_dtype)
# looks like garbage
# Attempt 2
np.array([x.split() for x in dat], dtype=dat_dtype)
# looks like different garbage
# Attempt 3
string_ndarray = np.array([x.split() for x in dat], dtype='|S15')
# looks good so far
string_ndarray.astype(dat_dtype)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1.040000e+005'
我放弃了。这是我获得预期输出的唯一方法:
dat_ndarray = np.zeros(len(dat), dat_dtype)
for i, line in enumarate(dat):
dat_ndarray[i] = tuple(line.split())
print(dat_ndarray) # [(1, 2, 104000.0, 0.03) (2, 7, 0.0, 0.03) (3, 15, 0.0, 0.03)]
有没有更直接的方法来获取预期的记录数组?
【问题讨论】:
-
嗯,你还应该指出你期望的输出是什么。