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!
谢谢大家!
3 个解决方案
#1
9
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]
文件
#2
3
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
#3
0
The first is:
首先是:
from functools import *
def wordParts (currentPart, lastPart):
return currentPart+lastPart;
word = ['Che', 'mis', 'try']
print(reduce(wordParts, word))
#1
9
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]
文件
#2
3
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
#3
0
The first is:
首先是:
from functools import *
def wordParts (currentPart, lastPart):
return currentPart+lastPart;
word = ['Che', 'mis', 'try']
print(reduce(wordParts, word))