How to read an array of integers from single line of input in python3

前端 未结 4 756
情深已故
情深已故 2020-11-30 09:37

I want to read an array of integers from single line of input in python3. For example: Read this array to a variable/list

1 3 5 7 9

What I

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 10:04

    You can try this below code that takes an input from user and reads it as array and not list.

    from array import *
    a = array('i',(int(i) for i in input('Enter Number:').split()))
    print(type(a))
    print(a)
    

    IN addition , if you wish to convert it to a list:

    b = a.tolist()
    print(type(b))  
    print(b)
    

提交回复
热议问题