1

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?

1 Answer 1

2

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

2
  • It's probably worthwhile to mention that specifying dtype results in datatype conversion from strings to ints under the hood.
    – alko
    Commented Dec 20, 2013 at 9:27
  • @alko, Thank you for comment. I updated the answer with quotation from the documentation.
    – falsetru
    Commented Dec 20, 2013 at 9:38

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.