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

Sign up to request clarification or add additional context in comments.

2 Comments

It's probably worthwhile to mention that specifying dtype results in datatype conversion from strings to ints under the hood.
@alko, Thank you for comment. I updated the answer with quotation from the documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.