?print-pdf
' Created for
map()
Functionmap()
Functionmap()
The map() function in Python is a tool for transforming elements within an iterable.
map(function, iterable, ...)
numbers = [1,2,3,4,5]
sqr_numbers = map(lambda x:x**2, numbers)
print( list(sqr_numbers) )
#[1, 4, 9, 16, 25]
letters = map(chr, range(1040, 1072))
print( list(letters) )
#['А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я']
letters = [chr(code) for code in range(1040, 1072)]
print( list(letter
#['А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я']
l1 = [1,2,3]
l2 = [1,2,3]
l_sum = map(lambda a,b: a+b, l1, l2)
print( list(l_sum) )
# [2, 4, 6]
filter()
Functionfilter()
Functionfilter()
is a built-in function that allows to process an iterable and extract those items that satisfy a given condition
filter(function, iterable)
def is_even(x):
return x % 2 == 0
evens = filter(is_even, range(1, 11))
print(list(even_numbers))
# [2, 4, 6, 8, 10]
evens = filter(lambda x: x % 2 == 0, range(1, 11))
print(list(evens))
# [2, 4, 6, 8, 10]
names = ["Ivan", "", "Alex", "", "Maria", "Angel", ""]
not_empty_names = filter(None, names)
print(list(not_empty_names))
["Ivan", "Alex", "Maria", "Angel"]
names = ["Ivan", "", "Alex", "", "Maria", "Angel", ""]
not_empty_names = [ name for name in names if name]
print( list(not_empty_names) )
names = ["Ivan", "Alex", "Maria", "Angel", ""]
filtered_names = filter(lambda name: name.startswith("A"), names)
print(list(filtered_names))
names = ["Ivan", "Alex", "Maria", "Angel", ""]
filtered_names = [name for name in names if name.startswith("A")]
print(list(filtered_names))
reduce()
Functionreduce()
Functionreduce()
allows us to apply a function to an iterable and reduce it to a single cumulative value
from functools import reduce
reduce(function, iterable[, initializer])
functools
module!reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
calculates ((((1+2)+3)+4)+5)reduce(lambda x, y: x+y, [1, 2, 3, 4, 5], 100)
calculates (((((100+1)+2)+3)+4)+5)
from functools import reduce
res = reduce(lambda a,b: a+b, range(11))
print(res)
#55
print(sum(range(11)))
products = [
{'name':'apples', 'price': 2},
{'name':'oranges', 'price': 5},
{'name':'bananas', 'price': 3},
]
#note that here we must provide initializer
total_price = reduce( lambda price, product:price+product['price'], products, 0)
print(total_price)
total_price = sum([p["price"] for p in products])
from functools import reduce
l1 = [1, 2, 3]
l2 = [1, 2, 3]
l3 = [1, 2, 3]
res = map(lambda *t: reduce(lambda a, c: a + c, t), l1, l2, l3)
print(list(res))
# [3, 6, 9]
list(map(lambda x, y: x + y, [1, 2, 3], [4, 5, 6])) # [5, 7, 9]
# This doesn't build the whole list in memory
result = map(process_function, very_large_dataset)
filtered = filter(lambda x: x > 1000, huge_dataset) # Iterator, not list
squares = [x**2 for x in numbers] # vs. map(lambda x: x**2, numbers)
squares = [x**2 for x in range(10)] # Ready to use list
even_squares = [x**2 for x in numbers if x % 2 == 0]
# vs
even_squares = map(lambda x: x**2, filter(lambda x: x % 2 == 0, numbers))
These slides are based on
customised version of
framework