LoginSignup
0
1

More than 1 year has passed since last update.

[Python]標準入力

Posted at

単要素

s = input()
n = int(input())

一行に複数

a b c

みたいなかんじ
split()空白区切りで要素を切り分ける関数。
引数に文字を与えればその文字で切り分けてくれる。

map()高階関数といい、「リストのすべての要素に同じ処理をします」というもの。
第一引数に関数を与え、第二引数に処理するリストを与える。

返り値はmapオブジェクト。このままではたまに面倒なので、list()を使って使いやすいリストオブジェクトに加工している。

s_list = input().split()
n_list = list(map(int, input().split()))

複数行に一つ

n
a
b
c

みたいなかんじ。たいてい要素数としてnが渡される。
[]に囲まれたfor文はリスト内包表記というもの。今回はn回分与えた関数を繰り返している。

n = int(input())
s_list = [input() for i in range(n)]
n_list = [int(input()) for i in range(n)]

複数行に複数(二次元配列)

↑でやったことの組み合わせ。

n = int(input())
s_2d = [input().split() for i in range(n)]
n_2d = [list(map(int, input().split()] for i in range(n)]
0
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
0
1