This article is almost the same as the article posted on my Hatena Blog.
I'm doing competitive programming, which has become a hot topic recently (don't look for it because it's a very weak coder ... crying).
In C ++, you can just connect >>
after std :: cin
(prejudice), but Python doesn't, so here are some ways to receive standard input.
The code below has been tested with Python 3.7.3
.
If you have any mistakes, please let me know! !!
In the competitive programming standard, the explanation is centered on integers, but I think that the basic structure does not change whether you receive integers or strings.
Behavior of ʻinput ()`
Receive standard input (1 line)
One character (string)
An integer with one input
Input is multiple integers
Receive standard input (multiple lines)
One integer for each line (number of lines specified)
Multiple integers for each row (number of rows and columns specified)
One integer for each line (no number of lines specified)
What I want to write about standard input
At the end
input([prompt])
If the argument prompt is present, it will be written to standard output except for trailing newlines. This function then reads a line from the input, converts it to a string and returns it (except for the trailing newline). EOFError is thrown when EOF is loaded.
Source: Python 3.7.4 documentation
(^ o ^) ………… Hey.
For the time being, what you should hold down when competing professionally
Read 1 line </ b> from input
Convert to string </ b> and return
I think there are two. With this in mind, let's look at an actual use case.
input
apple
Code 1
s = input()
print(s)
Output 1
apple
The type of the variable s
is, of course, the str
type.
If you want to list each character
Code 2
s = list(input())
print(s)
Output 2
['a', 'p', 'p', 'l', 'e']
The type of the variable s
is the list
type.
input
334
Code 3
n = int(input())
print(n)
Output 3
334
Since ʻinput ()returns
334 of type
str, it is enclosed in ʻint
.
Therefore, the type of the variable n
is ʻint` type.
Of course, even if the input is a number, if you want to treat it like a character string, you can write it as code 1 or 2 above.
It is overwhelmingly frequently used in competition professionals. Use a function called map
.
Input (each number is separated by a space)
334 -334 364
Code 4
a, b, c = map(int, input().split())
print(a, b, c)
Output 4
334 -334 364
If you write down what you are doing
ʻinput (). split ()returns a list with each integer separated by a half-width space as an element (in the above example,
['334','-334',, '364'] `). I wrote "integer as an element", but Python says these are still a list of strings.
map
is simply a function that applies the same processing to all elements of a list (or something). Here, the integers treated as strings are converted to ʻint` type.
Insert each element into the variable ʻa
b`` c`.
In Code 4, I prepared three variables and put them in each, but of course you can also list them.
Code 5
a = list(map(int, input().split()))
print(a)
Output 5
[334, -334, 364]
One thing to note is that you have to enclose it in list
and convert it to a list
type. This is because the return value of map
is not a list, but a map object , something that is difficult for a fucking programmer to understand </ s>.
When receiving multiple strings </ b>, it is almost the same as codes 4 and 5. However, if it remains ʻint,
ValueError Let's make it
str` properly because it spits.
(Added on 2019/12/31) </ b> When receiving multiple strings, you can simply use ʻinput (). Split ()instead of changing from
map to
list`.
Basically, you just repeat the above method for the number of lines, so you can just jump into the for loop and spin it.
Here, we will describe the method using the comprehension notation.
Most of the competition pros give you the number of lines to enter first, so here we will show you how to enter according to that.
input
5
441
484
529
576
625
Code 6
n = int(input())
l = [int(input()) for i in range(n)]
print(n, l)
Output 6
5 [441, 484, 529, 576, 625]
Of course, you can use it for strings by removing ʻint that encloses ʻinput ()
.
This is the method (link) that has recently been posted on Twitter.
Code 7
n, *l = map(int, open(0))
Similar to output 6.
However, when testing in a local environment, if you run the program and then enter the standard input, there is a high possibility that it will not work. Honestly, I'm not sure what I'm doing, so I'll leave the reference to code 7 so much. Asakugaku is out ... (already out).
It seems to be used for inputting problems on a two-dimensional plane or problems in which what appears has multiple elements.
Rotate code 5 in list comprehensions.
input
5 9
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
Code 8
n, m = map(int, input().split())
l = [list(map(int, input().split())) for i in range(n)]
print(n, m, l)
Output 8
5 9 [[1, 2, 3, 4, 5, 6, 7, 8, 9], [2, 4, 6, 8, 10, 12, 14, 16, 18], [3, 6, 9, 12, 15, 18, 21, 24, 27], [4, 8, 12, 16, 20, 24, 28, 32, 36], [5, 10, 15, 20, 25, 30, 35, 40, 45]]
I'm sorry it's hard to see, but you can see that the variable l
contains a two-dimensional list. The number of columns is not used in Python. You did it.
I think this is more of a practical aspect than a competitive professional.
input
441
484
529
576
625
Code 9
import sys
array = []
for i in sys.stdin.readlines():
array.append(int(i.rstrip()))
print(array)
Output 9
[441, 484, 529, 576, 625]
If I confess, I will not use it in the competition pro, so I thought about it now. Is it really right ...?
Code 9 is also likely to not work well if you run the program and then enter it as standard in a local environment.
The big difference between ʻinput ()and
sys.stdin.readline () is whether or not it contains a trailing newline character. ʻInput ()
excludes the trailing newline character, while sys.stdin.readline ()
contains the newline character. So when using sys.stdin.readline ()
, you often use rstrip ()
to strip the newline character (see Gugu for more details).
It seems that list comprehension is faster than writing for loops and using list comprehension (reference article: https://qiita.com/intermezzo-fr/items/43f90e07e4cebe63aeb6)
It was unexpectedly long when I wrote it. As I wrote at the beginning, please let me know if you have any mistakes!