Converting string series to float list in python

后端 未结 1 1729
我寻月下人不归
我寻月下人不归 2020-11-29 05:14

I am quite new to programing so I hope this question is simple enough.

I need to know how to convert a string input of numbers separated by spaces on a single line:<

相关标签:
1条回答
  • 2020-11-29 05:54

    Try a list comprehension:

    s = '5.2 5.6 5.3'
    floats = [float(x) for x in s.split()]
    

    In Python 2.x it can also be done with map:

    floats = map(float, s.split())
    

    Note that in Python 3.x the second version returns a map object rather than a list. If you need a list you can convert it to a list with a call to list, or just use the list comprehension approach instead.

    0 讨论(0)
提交回复
热议问题