python 문법심화2
- map,filter, lambda
people = [
{'name': 'bob', 'age': 20},
{'name': 'carry', 'age': 38},
{'name': 'john', 'age': 7},
{'name': 'smith', 'age': 17},
{'name': 'ben', 'age': 27},
{'name': 'bobby', 'age': 57},
{'name': 'red', 'age': 32},
{'name': 'queen', 'age': 25}
]
def check_adult(person):
if person['age'] > 20:
return '성인'
else:
return '청소년'
result = map(check_adult, people)
print(list(result)) #['청소년', '성인', '청소년', '청소년', '성인', '성인', '성인', '성인']
def check_adult(person):
return '성인' if person['age'] > 20 else'청소년'
result = map(check_adult, people)
print(list(result)) #['청소년', '성인', '청소년', '청소년', '성인', '성인', '성인', '성인']
map(함수 , 리스트)
result = map(lambda x: ('성인' if x['age'] > 20 else '청소년'), people)
filter(함수, 리스트)
result = filter(lambda x : (x['age'] >20),people)
print(list(result))