python - 从列表创建多维 numpy 数组

标签 python python-2.7 numpy

我有三个列表,

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]])

根据numpy.array documentation :

dtype : data-type, optional

The 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/

相关文章:

algorithm - NumPy:均匀分布的N维样本

python - 有人可以解释这些行 : X1, y1 = np.c_[np.random.normal(loc=new_center[0],

python - 使用 Pandas 中的日期时间索引滚动前瞻总和

python - 加速 Python 迭代

python - 禁用 Python 的 re.findall() 的 "group becomes tuple"行为

python - 使用两个变量优化 cumprod()

python - Pytest 使用 fixture 参数化测试

python - 在 pandas 中切片 DataFrame?

python - 如何查看python是否在控制台或shell中运行

python - numpy 是否在其*所有*功能中广播?