-2

I have a string of numbers like this

i = '584,569.2,11515,632'

want to convert it to list of number like this.

[584,569.2,11515,632]
Amir Shabani
  • 3,857
  • 6
  • 30
  • 67

1 Answers1

4

You can do it like this:

i = '584,569.2,11515,632'
numbers = list(map(float, i.split(',')))
print(numbers)

Output:

[584.0, 569.2, 11515.0, 632.0]

Also, as Chris A pointed out, if the distinction between int and float is important, you can use is_integer():

numbers = [int(x) if x.is_integer() else x for x in map(float, i.split(','))]

Output:

[584, 569.2, 11515, 632]
Amir Shabani
  • 3,857
  • 6
  • 30
  • 67