Write a function which takes a list as argument and decrease every value of list by 20 and return (python)
Answers
Hope it helps
Consider following
If appropriate Mark As Brainliest
Explanation:
The map() Function
As we have mentioned earlier, the advantage of the lambda operator can be seen when it is used in combination with the map() function. map() is a function which takes two arguments:
r = map(func, seq)
The first argument func is the name of a function and the second a sequence (e.g. a list) seq. map() applies the function func to all the elements of the sequence seq. Before Python3, map() used to return a list, where each element of the result list was the result of the function func applied on the corresponding element of the list or tuple "seq". With Python 3, map() returns an iterator.
The following example illustrates the way of working of map():
def fahrenheit(T): return ((float(9)/5)*T + 32) def celsius(T): return (float(5)/9)*(T-32) temperatures = (36.5, 37, 37.5, 38, 39) F = map(fahrenheit, temperatures) C = map(celsius, F) temperatures_in_Fahrenheit = list(map(fahrenheit, temperatures)) temperatures_in_Celsius = list(map(celsius, temperatures_in_Fahrenheit)) print(temperatures_in_Fahrenheit) print(temperatures_in_Celsius)
[97.7, 98.60000000000001, 99.5, 100.4, 102.2] [36.5, 37.00000000000001, 37.5, 38.00000000000001, 39.0]
In the example above we haven't used lambda. By using lambda, we wouldn't have had to define and name the functions fahrenheit() and celsius(). You can see this in the following interactive session:
C = [39.2, 36.5, 37.3, 38, 37.8] F = list(map(lambda x: (float(9)/5)*x + 32, C)) print(F)
[102.56, 97.7, 99.14, 100.4, 100.03999999999999]
C = list(map(lambda x: (float(5)/9)*(x-32), F)) print(C)
[39.2, 36.5, 37.300000000000004, 38.00000000000001, 37.8]
map() can be applied to more than one list. The lists don't have to have the same length. map() will apply its lambda function to the elements of the argument lists, i.e. it first applies to the elements with the 0th index, then to the elements with the 1st index until the n-th index is reached:
a = [1, 2, 3, 4] b = [17, 12, 11, 10] c = [-1, -4, 5, 9]
list(map(lambda x, y, z : x+y+z, a, b, c))
Output::
[17, 10, 19, 23]
list(map(lambda x, y : x+y, a, b))
Output::
[18, 14, 14, 14]
list(map(lambda x, y, z : 2.5*x + 2*y - z, a, b, c))
Output::
[37.5, 33.0, 24.5, 21.0]
If one list has fewer elements than the others, map will stop when the shortest list has been consumed:
a = [1, 2, 3] b = [17, 12, 11, 10] c = [-1, -4, 5, 9] list(map(lambda x, y, z : 2.5*x + 2*y - z, a, b, c))
Output::
[37.5, 33.0, 24.5]
We can see in the example above that the parameter x gets its values from the list a, while y gets its values from b, and z from list c.
Mapping a List of Functions
The map function of the previous chapter was used to apply one function to one or more iterables. We will now write a function which applies a bunch of functions, which may be an iterable such as a list or a tuple, for example, to one Python object.
from math import sin, cos, tan, pi def map_functions(x, functions): """ map an iterable of functions on the the object x """ res = [] for func in functions: res.append(func(x)) return res family_of_functions = (sin, cos, tan) print(map_functions(pi, family_of_functions))
[1.2246467991473532e-16, -1.0, -1.2246467991473532e-16]
The previously defined map_functions function can be simplified with the list comprehension technique, which we will cover in the chapter list comprehension:
def map_functions(x, functions): return [ func(x) for func in functions ]