How do you convert a list of strings to a list of floats using Python?

后端 未结 2 606
误落风尘
误落风尘 2021-01-29 09:01

If I have a list of strings:

[\'   3.2 4.5 7.8 9.2 4.3 4.7 5.2\',
\'   3.1 4.1 1.3 8.2 4.1 3.2 3.1\',
\'   3.1 4.2 5.7 3.2 4.1 3.0 1.9\']

How c

2条回答
  •  感动是毒
    2021-01-29 09:35

    You can use below code:

    numbers = ['   3.2 4.5 7.8 9.2 4.3 4.7 5.2',
    '   3.1 4.1 1.3 8.2 4.1 3.2 3.1',
    '   3.1 4.2 5.7 3.2 4.1 3.0 1.9']
    
    print([[float(number) for number in number_list.split()] for number_list in numbers])
    

提交回复
热议问题