how to convert text values in an array to float and add in python -
i have text file containing values:
120 25 50 149 33 50 095 41 50 093 05 50 081 11 50
i extracted values in first column , put array: adjusted
how convert values text float , add 5 each of them using for loop?
my desired output :
125 154 100 098 086
here code:
adjusted = [(adj + (y)) y in floats] a1 = adjusted[0:1] a2 = adjusted[1:2] a3 = adjusted[2:3] a4 = adjusted[3:4] a5 = adjusted[4:5] print a1 print a2 print a3 print a4 print a5 a11= [float(x) x in adjusted] fbearingbc = 5 + float(a11) print fbearingbc
it gives me errors says cant add float , string pliz help
assuming have:
adjusted = ['120', '149', '095', ...]
the simplest way convert float , add 5 is:
converted = [float(s) + 5 s in adjusted]
this create new list, iterating on each string s
in old list, converting float
, adding 5
.
assigning each value in list separate name (a1
, etc.) not way proceed; break if have more or fewer entries expected. cleaner , less error-prone access each value index (e.g. adjusted[0]
) or iterate on them (e.g. for in adjusted:
).
Comments
Post a Comment