-3

So I have and array named data. There is contents inside that looks like this

data[0] = ['46.09', '24.76', '0.70', '0.53', '27.92', '0.00',]

I want to be able to loop through this section and extract only the numbers and toss the commas and apostrophes so my desired output would look like:

number[0] = 46.09
number[1] = 24.76
number[2] = 0.70
and so on........

any help will be greatly appreciated

Thank You!

4

1 回答 1

3

Sounds like all you are trying to do is loop over a list, and convert the strings to floats:

for elem in data[0]:
    val = float(elem)
    print val

Or you could just map them to floats:

number = map(float, data[0])

Or use a list comprehension:

number = [float(s) for s in data[0]]
于 2013-05-16T04:20:30.217 回答