String of numbers to a list of integers?

后端 未结 4 1812
抹茶落季
抹茶落季 2021-01-27 11:41

I\'ve been presented with the task of turning a string of mixed numbers (\"1 3 5 8 10\"), for example, and my goal is to put these numbers into a list as integers.

I ha

4条回答
  •  借酒劲吻你
    2021-01-27 12:07

    You can use regular expressions:

    import re
    s = "1 3 5 8 10"
    final_data = list(map(int, re.findall('\d+', s)))
    print(final_data)
    

    Output:

    [1, 3, 5, 8, 10]
    

提交回复
热议问题