Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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

share|improve this answer
    
It's probably worthwhile to mention that specifying dtype results in datatype conversion from strings to ints under the hood. –  alko Dec 20 '13 at 9:27
    
@alko, Thank you for comment. I updated the answer with quotation from the documentation. –  falsetru Dec 20 '13 at 9:38
    
Thanks a lot @falsetru –  Anand Dec 20 '13 at 9:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.