scientific_comp_projects/CODE/parallel_computing/functional_programming/second_lesson_filterFunc.py

33 lines
1.1 KiB
Python

# We will use a collection.
import collections
from pprint import pprint
Scientist = collections.namedtuple('Scientist', [
'name',
'field',
'born',
'nobel',
])
scientists = (
Scientist(name='Ada Lovelace', field='math', born=1815, nobel=False),
Scientist(name='Emmy Noether', field='math', born=1882, nobel=False),
Scientist(name='Marie Curie', field='physics', born=1867, nobel=True),
Scientist(name='Tu Youyou', field='chemistry', born=1930, nobel=True),
Scientist(name='Ada Yonath', field='chemistry', born=1939, nobel=True),
Scientist(name='Vera Rubin', field='astronomy', born=1928, nobel=False),
Scientist(name='Sally Ride', field='physics', born=1951, nobel=True),
)
doYouNobel = tuple(filter(lambda x: x.nobel is True, scientists))
#pprint(doYouNobel)
matos = tuple(filter(lambda x: x.field == 'math', scientists))
#pprint(matos)
# This is the way (python btw), list comprehension
doYouNobel2 = tuple([x for x in scientists if x.nobel is True])
# And we can even jump the list creation part !
doYouNobel2 = tuple(x for x in scientists if x.nobel is True)
pprint(doYouNobel2)