Python3 standard input (competition pro)

Introduction

This is a standard input memo used in competition pros and coding tests. The top is the input and the bottom is the source code.

Basic

Python3's built-in function ʻinput () is a function that gets the input from the keyboard as a character string. When the input becomes multiple lines as shown below, ʻinput () is fetched one line each time. Click here for details (Official document)

99
hi
N = input()
M = input()
# N = '99'
# M = 'hi'

One integer

N
N = int(input())

Two or more integers

N M
N, M = map(int, input().split())

Two or more integers (listing side by side separated by spaces)

A_1 A_2 ... A_N
A = list(map(int, input().split()))

It is also possible below (please refer to the last reference article etc. for operation)

*A, = map(int, input().split())

Two or more integers (listing line breaks on N lines)

N
A_1
A_2
...
A_N
N = int(input())
A = [int(input()) for _ in range(N)]

N rows and M columns integers

N
P_(1,1) P_(1,2) ... P_(1,M)
...
P_(N,1) P_(N,2) ... P_(N,M) 
N = int(input())
P = [list(map(int, input().split())) for _ in range(N)]

bonus

Divide into the first integer and the subsequent integers

K S_1 S_2 ... S_N
K, *S = list(map(int, input().split()))
# K = K
# S = [S_1, S_2, ... S_N]

The * S is said to be unpacked (on the list). The asterisk * in Python was described in detail in the following article.

-Reverse asterisk lookup for Python 3.x -[Introduction to Python 3] * (Asterisk) Summary of 1 function

Recommended Posts

Python3 standard input (competition pro)
[Python] Standard input
[Python] About standard input
Python standard input summary that can be used in competition pro
[Python3] Standard input [Cheat sheet]
Standard input / summary / python, ruby
Receiving standard input tips @ python
Matrix representation with Python standard input
Python Paiza-Various skill checks and standard input
Python3 standard input I tried to summarize
What seems to be a template of the standard input part of the competition pro in python3
Standard input summary
Atcoder standard input set for beginners (python)
[Python] Add comments to standard input files
Notes on using dict in python [Competition Pro]
Python: Use zipfile to unzip from standard input
Key input in Python
Competitive Pro Template (Python)
Competitive Pro with Python and VSCode-Simplification of standard input and automation of sample case judgment-
python input and output
Python audio input / output
Memorize Python commentary 4 --Input
Key input in Python
Standard input / output summary
Have python parse the json entered from the standard input
[Python] Change standard input from keyboard to text file
Can be used in competition pros! Python standard library
Which is better, python standard input receiving input () or sys.stdin?
Cisco Memorandum _ Python config input
Python 3.4 or later standard pip
Write standard input in code
Standard input with time limit
Python standard unittest usage notes
Part 1 of receiving standard input
[For beginners] Summary of standard input in Python (with explanation)
Python Input Note in AtCoder
Transposed matrix in Python standard
[For AtCoder] Standard input memo
python standard virtual environment venv
[Python] How to use input ()
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
[Competition Pro] Let Python play a stone-picking game with no winning method