【发布时间】:2019-10-31 16:20:02
【问题描述】:
假设我有一个字符串列表,我想将其转换为 numpy 数组。例如我有
A=A=['[1 2 3 4 5 6 7]','[8 9 10 11 12 13 14]']
print(A)
['[1 2 3 4 5 6 7]', '[8 9 10 11 12 13 14]']
我希望我的输出如下所示:2 x 7 的矩阵
[1 2 3 4 5 6 7;8 9 10 11 12 13 14]
到目前为止我尝试过的如下:
m=len(A)
M=[]
for ii in range(m):
temp=A[ii]
temp=temp.strip('[')
temp=temp.strip(']')
M.append(temp)
print(np.asarray(M))
但是我的输出如下:
['1 2 3 4 5 6 7' '8 9 10 11 12 13 14']
谁能帮我正确删除左右括号并将结果转换为浮点矩阵。
【问题讨论】:
-
你的字符串是从哪里来的?
-
@StephenRauch 使用 panda 在 python 中读取 csv 文件
标签: arrays string list numpy casting