카테고리 없음

[AI-4] Python List Comprehensions

얼룩도마뱀 2021. 3. 7. 21:42

Python List Comprehensions

nums = [1, 2, 3, 4]

result = [x*x for x in nums]
# result == [1, 4, 9, 16]

result = [x*x for x in nums if x % 2 == 0]
# result = [4, 16]

list(map(lambda x : x*x, nums))
# [1, 4, 9, 16]

list(map(lambda x : x*x, filter(lambda x : x % 2 == 0, nums)))
# [4, 16]