I wanted to learn about usage mapin python, and a google search led me to http://www.bogotobogo.com/python/python_fncs_map_filter_reduce.php , which I found useful.
One of the codes on this page uses the for loop and puts mapit into the loop in an interesting way, and the list used in the function mapactually takes a list of two functions. Here is the code:
def square(x):
return (x**2)
def cube(x):
return (x**3)
funcs = [square, cube]
for r in range(5):
value = map(lambda x: x(r), funcs)
print value
output:
[0, 0]
[1, 1]
[4, 8]
[9, 27]
[16, 64]
So, at this point in this tutorial, I thought, “well, if you can write this code with the function on the fly (lambda), then you could write it using a standard function with def.” So I changed the code to this:
def square(x):
return (x**2)
def cube(x):
return (x**3)
def test(x):
return x(r)
funcs = [square, cube]
for r in range(5):
value = map(test, funcs)
print value
, , , r . , . :
def square(x):
return (x**2)
def cube(x):
return (x**3)
def power(x):
return x(r)
def main():
funcs = [square, cube]
for r in range(5):
value = map(power, funcs)
print value
if __name__ == "__main__":
main()
, def power(x). , lambda x funcs.
, def, , ? python, , , .