Python generator and ‘yield’ statement
1 | import numpy as np |
Map, reduce and filter (lambda function)
Python map() function
map() function returns a list of the results after applying the given function to each item of a given iterable (list, tuple etc.)
Syntax:
1
map(func, iter)
Parameters:
func: It is a function to which map passes each element of given iterable.
iter: It is a iterable which is to be mapped.NOTE: You can pass one or more iterable to the map() function.
Python reduce() function
- The reduce(fun,seq) function is used to apply a particular function passed in its argument to all of the list elements mentioned in the sequence passed along.This function is defined in “functools” module.
- Working:
- At first step, first two elements of sequence are picked and the result is obtained.
- Next step is to apply the same function to the previously attained result and the number just succeeding the second element and the result is again stored.
- This process continues till no more elements are left in the container.
- The final returned result is returned and printed on console.
Python filter() function
The filter() method filters the given sequence with the help of a function that tests each element in the sequence to be true or not.
syntax:
1
filter(func, seq)
Parameters:
func: function that tests if each element of a sequence true or not.
seq: sequence which needs to be filtered, it can be sets, lists, tuples, or containers of any iterators.
Linear algebra with Numpy
Basic
1 | import numpy as np |
References
- map function: https://www.geeksforgeeks.org/python-map-function/
- reduce function: https://www.geeksforgeeks.org/reduce-in-python/
- filter function: https://www.geeksforgeeks.org/filter-in-python/