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

后端 未结 6 1024
不知归路
不知归路 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:12

    A simple line can be...

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

    As some one wisely corrected me, in python >=3, it shall become,

    print(list(map(int,x.split())))  
    

    It can also be user in earlier versions.

提交回复
热议问题