Convert string list to number list

  • Follow


On importing a space delimited text file,  I have an extracted list of zero-padded two-digit integers in the form: Amylist{"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"}  How do I convert these to real numbers or integers? Thanks - Kurt  I have reviewed the Mathematica documentation and a few books looking for the equivalent of a simple string-to-number function like vba's "Value".  Could not find an answer. Thanks.       

0
Reply Canopus56 1/18/2010 7:34:37 AM

On Jan 17, 11:34 pm, Canopus56 <canopu...@yahoo.com> wrote:
> On importing a space delimited text file,  I have an extracted list of 
zero-padded two-digit integers in the form: Amylist{"00", "01", "02", "03",
 "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"}  How do I convert these to 
real numbers or integers? Thanks - Kurt  I have reviewed the Mathematica 
documentation and a few books looking for the equivalent of a simple string-to-number function like vba's "Value".  Could not find an answer. Thanks .      

ToExpression[list] will work.

Or you could implement a stricter version:

Replace[
 Characters[list] /. Table[ToString[i] -> i, {i, 0, 9}],
 {valid : {__Integer} :> FromDigits[valid], _ -> $Failed},
 {1}
]

0
Reply Raffy 1/18/2010 10:41:18 AM



Hi,

if you do not want it to convert on input, you may still use e.g. 

ToExpression:

ToExpression@{"00", "01", "02", "03", "04", "05", "06", "07"}

Daniel





Canopus56 wrote:

> On importing a space delimited text file,  I have an extracted list of zero-padded two-digit integers in the form: Amylist{"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"}  How do I convert these to real numbers or integers? Thanks - Kurt  I have reviewed the Mathematica documentation and a few books looking for the equivalent of a simple string-to-number function like vba's "Value".  Could not find an answer. Thanks.       

> 



0
Reply dh 1/19/2010 10:13:52 AM

Am 18.01.2010 08:34, schrieb Canopus56:
> On importing a space delimited text file,  I have an extracted list
> of zero-padded two-digit integers in the form: Amylist{"00", "01",
> "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12",
> "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"}
> How do I convert these to real numbers or integers? Thanks - Kurt  I
> have reviewed the Mathematica documentation and a few books looking
> for the equivalent of a simple string-to-number function like vba's
> "Value".  Could not find an answer. Thanks.
> 

The function you are looking for is called ToExpression. You need to map
it to your list of strings:

amylist={"02", "03", "04", "05", "06", "07", "08", "09"}

Map[ToExpression,amylist]

or abbreviated:

ToExpression /@ amylist

hth,

albert

0
Reply Albert 1/19/2010 10:14:25 AM

Thanks to all who replied.  Even with ToExpression, I was trouble getting the string to numeric conversion to persistently take when replacing to a list. There was some weird runtime error where you could run and get: 

lstTriple =lstTriple/.{x_,y_,z_}->{x,ToExpression[y],z}

lstTriple;

NumericQ[listTriple[[3,2]]]
True

lstTriple; 


NumericQ[listTriple[[3,2]]]
False

After splitting out the column containing numeric string data explicitly to a new single variable list, then running the Map function using ToExpression, and then reassembling the list triple, would Mathematica recognize the numeric and not string format persistently. 

Thanks again for the help. - Kurt 


----- Original Message ----
From: dh <dh@metrohm.com>
To: Canopus56 <canopus56@yahoo.com>
Sent: Mon, January 18, 2010 4:06:33 AM
Subject: Re: Convert string list to number list

Hi,
if you do not want it to convert on input, you may still use e.g. ToExpression:
ToExpression@{"00", "01", "02", "03", "04", "05", "06", "07"}
Daniel


Canopus56 wrote:
> On importing a space delimited text file,  I have an extracted list of zero-padded two-digit integers in the form: Amylist{"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"}  How do I convert these to real numbers or integers? Thanks - Kurt  I have reviewed the Mathematica documentation and a few books looking for the equivalent of a simple string-to-number function like vba's "Value".  Could not find an answer. Thanks.      


      

0
Reply Canopus56 1/19/2010 10:14:47 AM

4 Replies
628 Views

(page loaded in 0.073 seconds)

Similiar Articles:













7/19/2012 9:00:35 PM


Reply: