简体   繁体   English

Javascript 的 reduce()、map() 和 filter() 在 Python 中的等价物是什么?

[英]What are Python's equivalent of Javascript's reduce(), map(), and filter()?

What are Python's equivalent of the following (Javascript):什么是 Python 的等价物(Javascript):

function wordParts (currentPart, lastPart) {
    return currentPart+lastPart;
}

word = ['Che', 'mis', 'try'];
console.log(word.reduce(wordParts))

and this:和这个:

var places = [
    {name: 'New York City', state: 'New York'},
    {name: 'Oklahoma City', state: 'Oklahoma'},
    {name: 'Albany', state: 'New York'},
    {name: 'Long Island', state: 'New York'},
]

var newYork = places.filter(function(x) { return x.state === 'New York'})
console.log(newYork)

lastly, this:最后,这个:

function greeting(name) {
    console.log('Hello ' + name + '. How are you today?');
}
names = ['Abby', 'Cabby', 'Babby', 'Mabby'];

var greet = names.map(greeting)

Thanks all!谢谢大家!

They are all similar, Lamdba functions are often passed as a parameter to these functions in python. 它们都很相似,Lamdba函数经常作为参数传递给python中的这些函数。

Reduce: 降低:

 >>> from functools import reduce
 >>> reduce( (lambda x, y: x + y), [1, 2, 3, 4]
 10

Filter: 过滤:

>>> list( filter((lambda x: x < 0), range(-10,5)))
[-10, -9, -8, -7, - 6, -5, -4, -3, -2, -1]

Map: 地图:

>>> list(map((lambda x: x **2), [1,2,3,4]))
[1,4,9,16]

Docs 文件

It is worth noting that this question has been answered at face value above with the accepted answer, but as @David Ehrmann mentioned in a comment in the question, it is preferred to use comprehensions instead of map and filter .值得注意的是,这个问题已经用公认的答案在上面得到了回答,但是正如@David Ehrmann 在问题的评论中提到的那样,最好使用理解而不是mapfilter

Why is that?这是为什么? As stated in "Effective Python, 2nd Edition" by Brett Slatkin pg.正如 Brett Slatkin pg 在“Effective Python, 2nd Edition”中所述。 108, "Unless you're applying a single-argument function, list comprehensions are also clearer than the map built-in function for simple cases. map requires the creation of a lambda function for the computation, which is visually noisy." 108,“除非你应用单一参数 function,对于简单的情况,列表理解也比map内置的 function 更清晰map需要创建一个lambda 88340984068 的计算,视觉上有噪音,88340984068。” I would add the same goes for filter .我会为filter添加同样的内容。

eg let's say I want to map and filter over a list to return the square of the items in the list, but only the even ones (this is an example from the book).例如,假设我想要 map 并过滤一个列表以返回列表中项目的平方,但只有偶数(这是书中的一个例子)。

Using the accepted answer's method of using lambdas:使用接受的答案的使用 lambdas 的方法:

arr = [1,2,3,4]
even_squares = list(map(lambda x: x**2, filter(lambda x: x%2 == 0, arr)))
print(even_squares) # [4, 16]

Using comprehensions:使用理解:

arr = [1,2,3,4]
even_squares = [x**2 for x in arr if x%2 == 0]
print(even_squares) # [4, 16]

So, along with others, I would advise using comprehensions instead of map and filter .因此,与其他人一起,我建议使用理解而不是mapfilter This question dives into it even further.这个问题进一步深入。

As far as reduce goes, functools.reduce still seems like the proper option.reduce而言, functools.reduce似乎仍然是正确的选择。

reduce(function, iterable[, initializer])

filter(function, iterable)

map(function, iterable, ...)

https://docs.python.org/2/library/functions.html https://docs.python.org/2/library/functions.html

The first is: 首先是:

from functools import *
def wordParts (currentPart, lastPart):
    return currentPart+lastPart;


word = ['Che', 'mis', 'try']
print(reduce(wordParts, word))

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

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