9
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

競プロ用のチートシート(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(map(int, 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(map(int, 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$
例:123 $\rightarrow$ "123"

s = str(n)

$N$ $\rightarrow$ $L_N$
例:123 $\rightarrow$ [1, 2, 3]

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

$N$ $\rightarrow$ $L_S$
例:123 $\rightarrow$ ["1", "2", "3"]

l_s = list(str(n))

$S$ $\rightarrow$ $N$
例:"123" $\rightarrow$ 123

n = int(s)

$S$ $\rightarrow$ $L_N$
例:"123" $\rightarrow$ [1, 2, 3]

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

$S$ $\rightarrow$ $L_S$
例:"123" $\rightarrow$ ["1", "2", "3"]

l_s = list(s)

$L_N$ $\rightarrow$ $N$
例:[1, 2, 3] $\rightarrow$ 123

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

$L_N$ $\rightarrow$ $S$
例:[1, 2, 3] $\rightarrow$ "123"

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

$L_N$ $\rightarrow$ $L_S$
例:[1, 2, 3] $\rightarrow$ ["1", "2", "3"]

L_s = list(map(str, l_n))

$L_S$ $\rightarrow$ $N$
例:["1", "2", "3"] $\rightarrow$ 123

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

$L_S$ $\rightarrow$ $S$
例:["1", "2", "3"] $\rightarrow$ "123"

s = "".join(l_s)

$L_S$ $\rightarrow$ $L_N$
例:["1", "2", "3"] $\rightarrow$ [1, 2, 3]

l_n = list(map(int, l_s))
9
4
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
9
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?