481
449

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

競プロ等におけるpython3の標準入力

Last updated at Posted at 2017-10-25

しょっちゅうわからなくなるのでチートシート作成。
必要を感じたら追加します。


入力:改行型int

1
2
1
6
1

コード

a = [int(input()) for i in range(5)]

※行数が不明な場合

import sys
a = []
for l in sys.stdin:
    a.append(int(l))

print(a)

[1, 2, 1, 6, 1]

入力:改行型文字列

aa
ii
uu
ee
oo

コード

a = [input() for i in range(5)]

print(a)

[aa, ii, uu, ee, oo]

入力:連続文字int

10 20 00

コード

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

print(a)

[10, 20, 0]

入力:連続文字列

aa ii uu

コード

a = input().split()

print(a)

[10, 20, 0]

入力:複数行2数値

1 2
3 4
5 6
7 8
9 0

コード (2020/4/11 より良い方法をコメントにていただき変更しました。)

xy = [map(int, input().split()) for _ in range(5)]
x, y = [list(i) for i in zip(*xy)]

print(x)
print(y)
print(x[1]+y[1])

[1, 3, 5, 7, 9]
[2, 4, 6, 8, 0]
7

入力:複数行複合。リスト一つで処理。

1 a
3 b
5 c
7 d
9 e

コード

list = []
for i in range(5):
    a,b=input().split()
    list.append((int(a), b))

print(list[0][0]+list[1][0])
print(list[1][1])

4
b

入力:行列

1 2 3 4
5 6 7 8
1 1 1 1

コード

a = [input().split() for l in range(3)]

print(a)

[['1', '2', '3', '4'], ['5', '6', '7', '8'], ['1', '1', '1', '1']]

入力:単数値。桁ごとに分割したい。

12345678

コード

s = input()
a = [int(c) for c in s]

print(a[1])

2

入力:複数行数値。ここから正方形の行列を作成

123
456
789

コード

import sys
a = [[int(c) for c in l.strip()] for l in sys.stdin]

print(a)
print(a[1][1])

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
5

481
449
4

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
481
449

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?