Startup Data scientist Blog

データ分析系のテック情報を発信します

python forとifを使ったフィルター処理

python forとifを使った簡単なフィルター処理

input = [("Mary", 27), ("Joe", 30), ("Ruth", 43), ("Boe", 17),("Jenny", 22)]

youngPeople = []

for (person, age) in input:
    if age < 30:
        youngPeople.append(person)
    else:
        print(person + 'is too old!')
       
print('There are ' + str(len(youngPeople)) + ' young people')

Output

>>

Joeis too old!
Ruthis too old!
There are3 young people