String of numbers to a list of integers?

后端 未结 4 1811
抹茶落季
抹茶落季 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 11:56

    def iq_test(numbers):
        nums = []
        for num in numbers.split():
            nums.append(int(num)) 
    

    I guess ... you could(and probably should) just do a list comprehension instead

     nums = [int(num) for num in numbers.split()]
    

提交回复
热议问题