1
0

数値の出現率 Python3編

Posted at

0 以上 9 以下の整数が N 個与えられます。各数値の出現回数を求め、「0」の出現回数、「1」の出現回数、...「9」の出現回数、をこの順に半角スペース区切りで1行に出力してください。

前回と比べたら難しくはないかな。。。

N = int(input())
A = list(map(int,input().split()))
counts =[]
for i in range(10):
    count = 0
    for j in A:
        if i == j:
            count += 1
    counts.append(count)
print(*counts)

答えだと、
countを配列にして、それぞれのインデックス(0~9)ごとに
数値を加算する方法だった。
ちょっと一瞬わかりづらいかも?

print(*counts)もよいが、
print(" ".join(map(str, count)))
展開のやり方も覚えておこう

N = int(input())
A = [int(x) for x in input().split()]

count = [0] * 10
for num in A:
    count[num] += 1

print(" ".join(map(str, count)))

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