Search code examples
pythonarrayslistinputsys

Use sys.stdin.readlines() to create an array of integers


I want to create an array out of an input so I use sys.stdin.readlines(). My inputs are multiple integers looking like this:

random number
random number
random number 
random number

However sys.stdin.readlines() creates an array and turns all these random numbers into strings. The array looks like this:

Array = ['random number\n','random number\n','random number\n','random number\n']

That is not what I want. I want the input to be turnt into an array but I still want the array entries to be integers.

I use python so the arrays I am talking about are actually lists.


Solution

  • You'll have to convert them into integers using a comprehension or something similar e.g. [int(x) for x in sys.stdin.readlines()]