从列表创建多维numpy数组

2024-04-26 04:56:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有三张单子

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

有人能帮我吗?你知道吗


Tags: 代码importnumpy列表dataasnp数组
1条回答
网友
1楼 · 发布于 2024-04-26 04:56:51

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

根据^{} 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. ...

相关问题 更多 >

    热门问题