Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
162
rated 0 times [  168] [ 6]  / answers: 1 / hits: 24017  / 9 Years ago, tue, june 30, 2015, 12:00:00

What are Python's equivalent of the following (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!


More From » python

 Answers
84

They are all similar, lamdba functions are often passed as a parameter to these functions in 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


[#65990] Saturday, June 27, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
madelyn

Total Points: 449
Total Questions: 100
Total Answers: 100

Location: Seychelles
Member since Fri, May 7, 2021
3 Years ago
madelyn questions
Wed, Jul 28, 21, 00:00, 3 Years ago
Wed, Jul 14, 21, 00:00, 3 Years ago
Sat, Nov 7, 20, 00:00, 4 Years ago
Thu, Sep 3, 20, 00:00, 4 Years ago
;