0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AtCoder標準入力(Python)

Last updated at Posted at 2025-03-07

初めに

AtCoderを始めようと思ったので勉強ついでに備忘録を残しておく。処女作。

使用する関数やらの説明

input()関数

input()関数を用いると次のように文字列を入力できるようになる。
スクリーンショット 2025-03-06 211408.png
入力した文字列がinput()関数の戻り値になる
また、関数の引数に文字列を指定すると、入力エリアの前にその文章が表示される。
(画像で言うと、「好きな文字を入力」)
この文章を「プロンプト」と言う。必要なければ書かなくてOK(競プロではいらない)。

input()関数の戻り値は「str型」なので注意。

split()

文字列.split("区切り文字")で文字列を区切り文字で区切ったリストが戻り値として得られる。
,で区切りたい場合は以下のようにする。

#入力
s = "apple,banana,cheese"

#出力
s.split(",")
#['apple', 'banana', 'cheese']

split()関数の引数を指定しない時は,スペース,改行,タブが区切り文字になる。

#入力
s = "apple banana cheese"

#出力
s.split()
#['apple', 'banana', 'cheese']

リスト内包表記

繰り返し処理や条件分岐の結果をリストにしたい時、リスト内包表記を用いることでコードを1行でまとめることができる。また,処理も速くなる。

[処理 for 変数名 in イテラブル]の形で,繰り返し処理(for文)の結果をリストにできる。

#内包表記を使わずに
double1 = []
for x in range(5):
    double1.append(x*2)

#出力
print(double1)
#[0, 2, 4, 6, 8]

#内包表記を使って
double2 = [i*2 for i in range(5)]

#出力
print(double2)
#[0, 2, 4, 6, 8]

以下はこの記事の標準入力では扱わないが記しておく。

さらに条件分岐を使うときはif 条件式を末尾に加える。
[処理 for 変数名 in イテラブル if 条件式]

#内包表記を使わずに
odds1 = []
for i in range(10):
    if i % 2 == 1:
        odds1.append(i)

#出力
print(odds1)
#[1, 3, 5, 7, 9]

#内包表記を使って
odds2 = [i for i in range(6) if i % 2 == 1]

#出力
print(odds2)
#[1, 3, 5, 7, 9]

さらにelse文を使うときは次のように書く。if 条件式の位置が処理の後ろになっているので注意。
[処理(True) if 条件式 else 処理(False) for 変数名 in イテラブル ]


#内包表記を使わずに
odd10_1 = []
for i in range(10):
    if i % 2 == 1:
        odd10_1.append(i*10)
    else:
        odd10_1.append(i)

#出力
print(odd10_1)
#[0, 10, 2, 30, 4, 50, 6, 70, 8, 90]

#内包表記を使って
odds10_2 = [i*10 if i % 2 == 1 else i for i in range(10)]

#出力
print(odds10_2)
#[0, 10, 2, 30, 4, 50, 6, 70, 8, 90]

map()関数

map(関数,イテラブル)でイテラブルの要素にそれぞれ関数を適用したイテレータを返す。
リストで返ってくるわけではないので注意。

#入力
s = list(map(abs, [3,-8,1,0,7,-5]))
#出力
print(s)
#[3, 8, 1, 0, 7, 5]

標準入力(本題)

1行1列データ

入力

N

#str型(文字列)
s = input()
#int型(整数)
s = int(input())
#float型(小数)
s = float(input())

1行N列データ

入力

A B C

#文字列を1つづつ受け取り
A, B, C = input().split()
#文字列をlist型で受け取り
s = input().split()
#整数を1つづ受け取り
A, B, C = map(int, input().split())
#整数をlist型で受け取り
s = list(map(int, input().split()))

N行1列データ

入力

N
x1
x2



xN

input()は標準入力を1行づつ読み取るのに対して、sys.stdinは一括で読み取る。

N = int(input()))
x = [int(input()) for _ in range(N)]

#行数(N)が不明の時
import sys
x = []
for i in sys.stdin:
    x.append(int(i))

2行N列データ

入力

N
x1 x2 ・・・ xN
y1 y2 ・・・ yN

N = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))

2行N列データ

入力

N
x1 y1
x2 y2



xN yN

# x,yをそれぞれリスト化(zip(*xy)はxyの転置行列)
N = int(input())
xy = [map(int, input().split()) for _ in range(N)]
x, y = [list(i) for i in zip(*xy)]

# x[i] = xy[0][i], y[i]=xy[1][i]としてリスト化
N = int(input())
xy = [list(map(int, input().split())) for _ in range(N)]

#xがint,yがstrの時
N = int(input())
xy = []
for i in range(N):
    a, b = input().split()
    xy.append(int(a),b)

必要に応じて追加します。

参考にしたサイト

競プロ等におけるpython3の標準入力
初心者向けAtcoder標準入力セット(Python)
【Python 入門】リスト内包表記について解説!
【Python入門】split関数で文字列の分割の仕方

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?