Python中与JavaScript的reduce()、map()和filter()等价的是什么?

16 浏览
0 Comments

Python中与JavaScript的reduce()、map()和filter()等价的是什么?

Python中以下内容的等效方式是什么(与JavaScript相比):

def wordParts(currentPart, lastPart):
    return currentPart + lastPart
word = ['Che', 'mis', 'try']
print(reduce(wordParts, word))

以及:

places = [
    {'name': 'New York City', 'state': 'New York'},
    {'name': 'Oklahoma City', 'state': 'Oklahoma'},
    {'name': 'Albany', 'state': 'New York'},
    {'name': 'Long Island', 'state': 'New York'}
]
newYork = list(filter(lambda x: x['state'] == 'New York', places))
print(newYork)

最后,这个:

def greeting(name):
    print('Hello ' + name + '. How are you today?')
names = ['Abby', 'Cabby', 'Babby', 'Mabby']
greet = list(map(greeting, names))

谢谢大家!

0