0

I am trying to change the elements of a list of strings to floats using the method defined in this thread. I write

with open('posx_mean_no_acoplo_tf_multiple.txt', 'r') as fmediaXoriginal:
    contentmediaXoriginal = fmediaXoriginal.readlines()
    contentmediaXoriginal = [x.strip() for x in contentmediaXoriginal] 
    [float(i) for i in contentmediaXoriginal]

As specified in the other thread. However, if I write print(type(contentmediaXoriginal[2])), then, the output is <class 'str'>. As far as I can see, I am following the accepted answer to the letter. Can someone tell me why my code is not converting the elements of contentmediaXoriginal to floats?

The first 5 lines of posx_mean_no_acoplo_tf_multiple.txt are:

2.25
2.2695317544146922
2.329339980428795
2.4250625977456477
2.5550797011698574
Ganesh Tata
  • 1,118
  • 8
  • 26
slow_learner
  • 337
  • 1
  • 2
  • 15

1 Answers1

1

You are nearly there. You just have not assigned the output. Try this

with open('posx_mean_no_acoplo_tf_multiple.txt', 'r') as fmediaXoriginal:
contentmediaXoriginal = fmediaXoriginal.readlines()
contentmediaXoriginal = [float(x.strip()) for x in contentmediaXoriginal] 

Note: Please fix indentation as you require it.

OlorinIstari
  • 537
  • 5
  • 20