[SciPy-user] Convert a string list to array ?

Travis E. Oliphant oliphant at enthought.com
Tue Feb 26 17:46:18 EST 2008


Stef Mientki wrote:
> hello,
>
> I have a string list that I want to convert to an integer array
>   data_all = [ '12', '24' ]
>
> and now I would expect this would work, but it crashes:
>   data_all = asarray (  data_all, dtype = int )
>   
Yeah,   perhaps unexpectedly, the conversion code requires integers not 
strings when you specify dtype=int.

You can also do:

asarray(data_all).astype(int)

or

asarray([int(x) for x in data_all],dtype=int)

-Travis O.





More information about the SciPy-User mailing list