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?

More than 1 year has passed since last update.

Python 標準入力

Last updated at Posted at 2022-08-16

自分がatcoder などでpythonを使うときのためのメモ

二次元配列 数字 (スペースあり)

入力例

1 3 4 10
2 3 4 5
3 25 3 91
4 5 2 3
a = [list(map(int, input().split())) for _ in range(n)]

内包表記を使い、list(map(int, input().split()))をn回繰り返して入力している
〜細かく説明〜
list(map(int, input().split())) ・・・空白ありの数字を配列で標準入力する時のやつ
input().split()・・・split()の()の中にsplit(分裂)を決める文字を入れる。何も入れない時は(" ")これが自動で。(" ")は半角スペースでsplitさせるという意味。

二次元配列 数字 (スペースなし)

入力例

4783
1267
6532
9823

4784ではなく4 7 8 4 で二次元配列に格納したい時

a = [list(map(str, input())) for _ in range(n)]

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

str型で格納し、使うときはint(a[0][0])このように型変換して利用するのがベストかなと。

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