简体   繁体   English

更高阶的功能(地图)

[英]Higher Order of Function (Map)

I am slightly confused by map in python. 我对python中的map有点困惑。 The function for map accepts 2 parameters: `map(function, variables). map的函数接受2个参数:`map(函数,变量)。

Why is the code below able to take in multiply and add as variables but the second code isn't able to? 为什么下面的代码能够multiplyadd为变量,但第二个代码不能? In a usual case, multiply should be passed in in as a function, check out range. 在通常情况下, multiply应作为函数传入,检出范围。

def multiply(x):
    return x * x

def add(x):
    return x + x

funcs = [multiply, add]

for i in range (1, 5):
    value = list(map(lambda x: x(i), funcs))
    print(value)

This is the second code: 这是第二个代码:

def multiply(x):
    return x * x

def add(x):
    return x + x

funcs = (add, multiply)
multi_func = (multiply)

for i in range (1, 5):
    value = list(map(lambda x: x(i), multi_func))
    print(value)

Is it possible to make use of 1 function and still use for in range? 是否可以使用1功能并仍在范围内使用?

Using range: 使用范围:

map(multiply, range(1, 5))

map applies its first argument, which is a function, to each element of the iterable which is the second argument. map将第一个参数(一个函数)应用于iterable的每个元素,即第二个参数。 The function is applied lazily. 该功能懒洋洋地应用。 That means it's done only when you iterate over the map object, eg, when you create a list of of it. 这意味着只有在迭代地图对象时才会完成它,例如,当您创建它的列表时。

Let's take a look at your first code. 我们来看看你的第一个代码吧。 funcs = [multiply, add] creates a list, which is iterable, of two elements. funcs = [multiply, add]创建一个可迭代的两个元素的列表。 Both elements are functions. 这两个元素都是功能。 This is normal in Python, because functions are just regular objects, and can be passed around, assigned, have attributes, etc. The loop 这在Python中是正常的,因为函数只是常规对象,可以传递,分配,具有属性等。循环

for i in range (1, 5):
    value = list(map(lambda x: x(i), funcs))
    print(value)

Repeats form 1 to 4. At each iteration it maps lambda x: x(i) to the functions in funcs . 重复形式1到4.在每次迭代时,它将lambda x: x(i)映射到funcs的函数。 When i = 1 , the map ends up doing multiply(1), add(1) . i = 1 ,地图最终进行multiply(1), add(1) When i = 2 , it's multiply(2), add(2) , and so on. i = 2 ,它multiply(2), add(2) ,依此类推。

The second code doesn't work because of a typo. 由于输入错误,第二个代码不起作用。 (x) is just x , but (x,) is a one-element tuple whose first element is x . (x)只是x ,但是(x,)是一个元素元组,其第一个元素是x map requires the second argument to be iterable, so passing in a function won't do. map 要求第二个参数是可迭代的,所以传入一个函数是行不通的。 If you want to map to a single function, you need to supply an iterable with one element: multi_func = (multiply,) . 如果要映射到单个函数,则需要提供具有一个元素的iterable: multi_func = (multiply,)

Once corrected, the second version will print multiply(1) when i = 1 , multiply(2) when i = 2 , etc. 一旦纠正,第二版本将打印multiply(1)i = 1multiply(2)i = 2 ,等等。

Something like list(map(multiply, range(1, 5))) will in fact be an easier way to write the second version. list(map(multiply, range(1, 5)))的东西实际上是编写第二个版本的一种更简单的方法。 You can also do something similar with the first code, using zip : 您也可以使用zip来执行与第一个代码类似的操作:

zip(map(func, range(1, 5)) for func in funcs)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM