LoginSignup
3
1

競プロ用のチートシート(Python)

Last updated at Posted at 2024-03-16

はじめに

競プロで使う Python のチートシートを載せています。

標準入力

記号は次の意味で使い分けています。
整数:$N, M, A_i, B_i, H, W$
文字列:$S, T, S_i$

$N$

n = int(input())

$N$ $M$

n, m = map(int, input().split())

$N$
$A_1$ $A_2$ $\dots$ $A_N$

n = int(input())
a = list(map(int, input().split()))

$N$
$A_1$
$A_2$
$\vdots$
$A_N$

n = int(input())
a = [int(input()) for _ in range(n)]

$N$
$A_1$ $B_1$
$A_2$ $B_2$
$\vdots$
$A_N$ $B_N$

n = int(input())
ab = [list(input().split()) for _ in range(n)]

$H$ $W$
$A_{1,1}$ $A_{1,2}$ $\dots$ $A_{1,W}$
$A_{2,1}$ $A_{2,2}$ $\dots$ $A_{2,W}$
$\vdots$
$A_{H,1}$ $A_{H,2}$ $\dots$ $A_{H,W}$

h, w = map(int, input().split())
a = [list(input().split()) for _ in range(h)]

$S$

s = input()

$S$ $T$

s, t = input().split()

$N$
$S_1$
$S_2$
$\vdots$
$S_N$

n = int(input())
s = [input() for _ in range(n)]

型変換

記号は次の意味で使い分けています。
整数:$N$
文字列:$S$
整数のリスト:$L_N$
文字列のリスト:$L_S$

型変換は期待通りの結果にならない場合があります。
例えば、$S$ $\rightarrow$ $N$ では、 $S$ がアルファベットや漢字などアラビア数字以外の文字を含む場合は期待通りの結果になりません。

$N$ $\rightarrow$ $S$

s = str(n)

$N$ $\rightarrow$ $L_N$

l_n = list(map(int, str(n)))

$N$ $\rightarrow$ $L_S$

l_s = list(str(n))

$S$ $\rightarrow$ $N$

n = int(s)

$S$ $\rightarrow$ $L_N$

l_n = list(map(int, str(s)))

$S$ $\rightarrow$ $L_S$

l_s = list(s)

$L_N$ $\rightarrow$ $N$

n = int("".join(map(str, l_n)))

$L_N$ $\rightarrow$ $S$

s = "".join(map(str, l_n))

$L_N$ $\rightarrow$ $L_S$

L_s = list(map(str, l_n))

$L_S$ $\rightarrow$ $N$

n = int("".join(l_s))

$L_S$ $\rightarrow$ $S$

s = "".join(l_s)

$L_S$ $\rightarrow$ $L_N$

l_n = list(map(int, l_s))
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1