[Python] Standard input

Standard input

We have summarized the standard input method in Python assuming AtCoder etc.

String

Sample code


S = input() #Standard input here
print (type(S), S)
#<class 'str'> atcoder

integer

Sample code


N = int(input()) #Standard input here
print (type(N), N) 
#<class 'int'> 20

queue

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]]

Large amount of input

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

[Python] Standard input
[Python] About standard input
[Python3] Standard input [Cheat sheet]
Python3 standard input (competition pro)
Standard input / summary / python, ruby
Receiving standard input tips @ python
Python3 standard input for competitive programming
Standard input summary
Python Paiza-Various skill checks and standard input
Python3 standard input I tried to summarize
Atcoder standard input set for beginners (python)
[Python] Add comments to standard input files
Key input in Python
python input and output
Python audio input / output
Memorize Python commentary 4 --Input
Key input in Python
Standard input / output summary
Python: Use zipfile to unzip from standard input
Cisco Memorandum _ Python config input
Python 3.4 or later standard pip
Python
Write standard input in code
Standard input with time limit
Python standard unittest usage notes
Part 1 of receiving standard input
Have python parse the json entered from the standard input
Python Input Note in AtCoder
Transposed matrix in Python standard
[Python] Change standard input from keyboard to text file
[For AtCoder] Standard input memo
python standard virtual environment venv
Which is better, python standard input receiving input () or sys.stdin?
[Python] How to use input ()
[For beginners] Summary of standard input in Python (with explanation)
Install Python 3.8 on Ubuntu 18.04 (OS standard)
Tips on Python file input / output
Install Python 3.8 on Ubuntu 20.04 (OS standard)
Input / output with Python (Python learning memo ⑤)
[Python] Adjusted the color map standard
Make standard output non-blocking in Python
Comply with Python coding standard PEP8
Let's see using input in python
Notes for Python file input / output
Install Python 3.9 on Ubuntu 20.04 (OS standard?)
Useful to remember! 10 Python Standard Libraries
Install Python 2.7 on Ubuntu 20.04 (OS standard?)
RPC completed with standard Python3 modules
Python standard input summary that can be used in competition pro
Calculate and display standard weight with python
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
I compared python3 standard argparse and python-fire
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python memorandum
Data input / output in Python (CSV, JSON)