Change a string of integers separated by spaces to a list of int

后端 未结 6 1002
不知归路
不知归路 2020-12-25 11:33

How do i make something like

x = \'1 2 3 45 87 65 6 8\'

>>> foo(x)
[1,2,3,45,87,65,6,8]

I\'m completely stuck, if i do it by inde

6条回答
  •  被撕碎了的回忆
    2020-12-25 12:30

    No need to worry, because python provide split() function to change string into a list.

    x='1 2 3 4 67 8 9'
    x.split()
    

    ['1', '2', '3', '4', '67', '8']

    or if you want output in integer form then you can use map function

    map(int ,x.split(' '))
    

    [1, 2, 3, 4, 67, 8]

提交回复
热议问题