我有三个列表,
list1=['10','20','30']
list2=['40','50','60']
list3=['70','80','90']
我想从这些列表创建一个 numpy 数组。我正在使用以下代码:
import numpy as np
list1=['10','20','30']
list2=['40','50','60']
list3=['70','80','90']
data = np.array([[list1],[list2],[list3]])
print data
我得到的输出为:
[[['10' '20' '30']]
[['40' '50' '60']]
[['70' '80' '90']]]
但我期望输出为:
[[10 20 30]
[40 50 50]
[70 80 90]]
有人可以帮我吗?
最佳答案
指定dtype
:
>>> import numpy as np
>>> list1=['10','20','30']
>>> list2=['40','50','60']
>>> list3=['70','80','90']
>>> np.array([list1, list2, list3], dtype=int)
array([[10, 20, 30],
[40, 50, 60],
[70, 80, 90]])
dtype
: data-type, optionalThe desired data-type for the array. If not given, then the type will be determined as the minimum type required to hold the objects in the sequence. ...
关于python - 从列表创建多维 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20700002/