Michael Michael - 10 months ago 28
Python Question

Changing strings to float variables in a 2d array

I have a 2D array of strings called c. It has 3 columns. I want to make every 2nd column in each row a float type value. This is what I have so far

for row in range(len(c)):
cNew = float(c[i][1])


EDIT: I misread you question. Here's one solution:

array = [['string', '2.0', 'string'],
         ['string', '3.0', 'string']]

y = [ [float(x) if i%2 == 1 else x for i,x in enumerate(row)] for row in array]