-1

I would like to know how to convert a string like "1 -2 -8 4 5" into something like [1, -2, -8, 4, 5]

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Pablo Clsn
  • 13
  • 1
  • 6
  • ```I would like to know how to ...``` - Work your way through [The Tutorial](https://docs.python.org/3/tutorial/index.html) and you will start getting ideas how to solve the problem. Look through [the docs](https://docs.python.org/3/library/index.html) to get some more ideas. Try some of your ideas out. – wwii Mar 04 '17 at 16:02

3 Answers3

3

I'd split the string by the " " character, and convert each element to an int:

myList = [int(x) for x in myString.split(" ")]

or simply

myList = [int(x) for x in myString.split()]

by default split uses the white space ' ' as argument

Mureinik
  • 297,002
  • 52
  • 306
  • 350
0
array=list(map(int,string.split()))
Brad
  • 15,186
  • 11
  • 60
  • 74
Shihab Shahriar Khan
  • 4,930
  • 1
  • 18
  • 26
0
list(map(int , string.split(' ')))

Split with a space.

Afaq
  • 1,146
  • 1
  • 13
  • 25