1
0

More than 3 years have passed since last update.

Python で半角スペース区切りの整数を受け取るときの典型的な書き方

Posted at

Python で半角スペース区切りの整数を受け取るときの典型的な書き方

N, M, K = map(int, input().split())

・Python ではいくつかの変数を、リストを使ってまとめて初期化することができます。今回のように N, M, K = hoge と書くと、代入されるのが長さ 3 のリストであるとき、そのリストの 0, 1, 2 番目の要素がそれぞれ N, M, K に代入されます。

・N, M, K = map(int, input().split()) は Python で半角スペース区切りの整数を受け取るときの典型的な書き方のひとつです。右辺では input().split() の各要素に int関数 を適用しています。

N, M, K = map(int, input().split())

for i in range(N):
    a = [int(j) for j in input().split()]
    ans = 0
    for j in range(M):
        if a[j] == K:
            ans += 1
    print(ans)
1
0
1

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