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

前端 未结 4 759
情深已故
情深已故 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:15

    Use map:

    arr = list(map(int, input().split()))
    

    Just adding, in Python 2.x you don't need the to call list(), since map() already returns a list, but in Python 3.x "many processes that iterate over iterables return iterators themselves".

    This input must be added with () i.e. parenthesis pairs to encounter the error. This works for both 3.x and 2.x Python

提交回复
热议问题