We have summarized the standard input method in Python assuming AtCoder etc.
Sample code
S = input() #Standard input here
print (type(S), S)
#<class 'str'> atcoder
Sample code
N = int(input()) #Standard input here
print (type(N), N)
#<class 'int'> 20
Sample code
R = int(input()) #Enter the number of lines
P = [list( map( int, input().split() ) ) for i in range(R)] #Standard input from the second line onwards
# map(function,element)
print(type(P), P)
#<class 'list'> [[1, 32], [2, 63], [1, 12]]
Sample code
import sys
import itertools
import numpy as np
read = sys.stdin.buffer.read
#This is a function to get multiple lines from standard input. readlines()Returns the result as a list, split by line, while read()Gets the result as a single string.
readline = sys.stdin.buffer.readline
#A function to get one line from standard input.
readlines = sys.stdin.buffer.readlines
#This is a function to get multiple lines from standard input. The return value is a list, and the entered character string is stored as an element line by line.
# .The buffer returns bytes, so it seems a bit faster, but it doesn't make much difference.
#Standard input example
# 3 4 3
# 1 3 3 100
# 1 2 2 10
# 2 3 2 10
N, M, Q = map(int, readline().split())
print(N, M, Q)
# 3 4 3
#Numpy array of numbers(N×M)If you want to enter as, remove np and it will be a list.
#Because the character string is bytes type'b""'Is attached. It is better to remove the buffer from the read definition because it cannot be combined.
# input()Avoid using with.
A = np.array(list(map(int, read().split()))).reshape((N, M))
#Get a NumPy array of the same type as below
m = map(int, read().split())
#print(list(m))
# <map object at 0x7f78f13a7d30> [1, 3, 3, 100, 1, 2, 2, 10, 2, 3, 2, 10]
for a,b,c,d in zip(m,m,m,m): #A function that groups elements of multiple iterable objects (lists, tuples, etc.)
print(a,b,c,d)
# 1 3 3 100
# 1 2 2 10
# 2 3 2 10
Recommended Posts