Get list of integers using stdin.readline ()

0

I use this the following entry to enter a list of numbers, for example this list: 1 2 3 4 5 6

a=stdin.readline().strip().split()

If I give you print(a) it shows me ['1','2','3','4','5','6'] , my question is if you can modify that entry in such a way that it shows me the list of integers ( [1,2,3,4,5,6] ) but not of strings, or simply not Will it be?

    
asked by DDR 31.01.2018 в 07:06
source

1 answer

1

You must explicitly cast int , for example using compression lists:

from sys import stdin

a = [int(n) for n in stdin.readline().strip().split()]

Or using input() :

a = [int(n) for n in input().split()]

Keep in mind that if you enter a value that can not be transformed into int you will get an exception ( ValueError: invalid literal for int() with base 10 ), this includes substrings with the decimal point ('4.2', '7.25', etc. ). If you accept signed integers ('-3'. '-4', '+7', etc).

    
answered by 31.01.2018 / 07:42
source