Anand
Anand

Reputation: 853

Create multidimensional numpy array from lists

I have three lists,

list1=['10','20','30']

list2=['40','50','60']

list3=['70','80','90']

I want to create a numpy array from these lists. I am using the foloowing code:

import numpy as np
list1=['10','20','30']
list2=['40','50','60']
list3=['70','80','90']

data = np.array([[list1],[list2],[list3]])
print data

I am getting output as:

 [[['10' '20' '30']]
  [['40' '50' '60']]
  [['70' '80' '90']]]

But I am expecting output as:

[[10 20 30]
 [40 50 50]
 [70 80 90]] 

Can anybody plz help me on this?

Upvotes: 1

Views: 213

Answers (1)

falsetru
falsetru

Reputation: 368944

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

According to 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. ...

Upvotes: 2

Related Questions