1
0

More than 3 years have passed since last update.

python3 配列条件付き 抽出方法(簡易版)

Posted at

指定した配列(リスト)を定義し、それらの要素のうち5以上の数をすべて足し合わせた値を出力してください。

l = [4, 0, 5, -1, 3, 10, 6, -8]
l_plus = [i for i in l if i >= 5]

print(sum(l_plus))


改行区切りで整数がn個入力されるので、n個の整数のうち、5以上のものをすべて足し合わせた値を出力してください。

n = int(input())

a = [0]*n

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


a_list = [i for i in a if i >= 5]

print(sum(a_list))
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