Python及Javascript的map 、 filter 、reduce类似函数的对比汇总
A.
在Python中,map 和 filter 是两个非常有用的内置函数,它们分别用于对可迭代对象中的每个元素执行某种操作,并返回结果。在JavaScript中,虽然没有内置的 map 和 filter 函数,但是可以使用数组的 map() 和 filter() 方法来实现类似的功能。
Python 中的 map 和 filter
map()
map(function, iterable):对 iterable 中的每个元素应用 function,并返回一个迭代器(在Python 3中是迭代器,但在Python 2中是列表)。
numbers = [1, 2, 3, 4]
squared = map(lambda x: x**2, numbers)
print(list(squared)) # 输出: [1, 4, 9, 16]
filter()
filter(function, iterable):对 iterable 中的每个元素应用 function,返回一个迭代器(在Python 3中是迭代器,但在Python 2中是列表),只包含使 function 返回 True 的元素。
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
print(list(even_numbers)) # 输出: [2, 4, 6]
JavaScript 中的 map 和 filter
JavaScript的数组提供了 map() 和 filter() 方法,它们的行为与Python中的类似。
map()
array.map(function(currentValue, index, arr), thisValue):创建一个新数组,其结果是该数组中的每个元素都调用一个提供的函数后返回的结果。
const numbers = [1, 2, 3, 4];
const squared = numbers.map(x => x ** 2);
console.log(squared); // 输出: [1, 4, 9, 16]
filter()
array.filter(function(currentValue, index, arr), thisValue):创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。
const numbers = [1, 2, 3, 4, 5, 6];
const evenNumbers = numbers.filter(x => x % 2 === 0);
console.log(evenNumbers); // 输出: [2, 4, 6]
这两个方法在JavaScript和Python中都提供了强大的功能来处理数组或可迭代对象中的元素。在JavaScript中使用这些方法时,你可以直接操作数组而不需要转换为列表或迭代器,这在Python中通常需要显式转换。
在JavaScript中,reduce 方法是一种数组方法,它允许你通过一个回调函数来迭代整个数组,并将数组中的每个值累积到一个单一的返回值。这个方法非常有用,特别是在需要将数组中的所有元素合并成一个单一值时。
基本语法
arr.reduce(callback(accumulator, currentValue, index, array), initialValue)
callback:执行数组中每个值的函数,接收四个参数:
accumulator:累计器累积回调的返回值;它是上一次调用回调时返回的累积值,或者是提供的初始值(initialValue)。
currentValue:数组中正在处理的当前元素。
index(可选):数组中正在处理的当前元素的索引。
array(可选):调用 reduce 的数组。
initialValue(可选):传递给函数的初始累加器值。
示例
1. 求和
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum); // 输出:10
2. 合并数组元素为字符串
const letters = ['a', 'b', 'c'];
const str = letters.reduce((accumulator, currentValue) => accumulator + currentValue);
console.log(str); // 输出:"abc"
3. 对象数组累加属性值
const items = [{value: 1}, {value: 2}, {value: 3}];
const total = items.reduce((accumulator, currentItem) => accumulator + currentItem.value, 0);
console.log(total); // 输出:6
Python 中的类似功能
在Python中,虽然没有直接叫做 reduce 的内置函数,但你可以使用 functools 模块中的 reduce 函数来实现类似的功能。
Python 中的 reduce 使用示例:
from functools import reduce
numbers = [1, 2, 3, 4]
sum = reduce(lambda acc, x: acc + x, numbers)
print(sum) # 输出:10
在这个Python示例中,reduce 函数通过一个 lambda 函数来迭代列表,将列表中的所有元素累加。这与JavaScript中的 reduce 方法在功能上是等价的。
B.
Python和JavaScript在map
、reduce
、filter
等函数式编程语法上有以下相似之处:
1. 高阶函数的功能对应
-
map
:在两种语言中均用于对集合的每个元素进行转换。- Python:
map(func, iterable)
,返回迭代器。 - JavaScript:
array.map(func)
,返回新数组。
- Python:
-
filter
:用于筛选符合条件的元素。- Python:
filter(func, iterable)
,返回迭代器。 - JavaScript:
array.filter(func)
,返回新数组。
- Python:
-
reduce
:用于将集合元素累积为单个值。- Python:
functools.reduce(func, iterable[, initial])
。 - JavaScript:
array.reduce(func(acc, curr[, index, array])[, initial])
。
- Python:
2. 参数结构相似
- 函数在前,数据在后:两者都要求传入一个处理函数作为首个参数,数据(可迭代对象或数组)作为第二个参数(或方法调用主体)。
pythonCopy Code
# Python list(map(lambda x: x*2, [1,2,3])) # [2,4,6]
javascriptCopy Code
// JavaScript [1, 2, 3].map(x => x * 2); // [2,4,6]
3. 支持匿名函数
- 均可使用匿名函数(Python的
lambda
,JS的箭头函数)作为参数:pythonCopy Code
list(filter(lambda x: x % 2 == 0, [1,2,3])) #
javascriptCopy Code
[1, 2, 3].filter(x => x % 2 === 0); //
4. reduce
的初始值可选
- 在
reduce
中,初始值(initial
)均为可选参数:pythonCopy Code
from functools import reduce reduce(lambda a, b: a + b, [1,2,3], 0) # 6
javascriptCopy Code
[1, 2, 3].reduce((acc, curr) => acc + curr, 0); // 6
5. 链式操作的思想
- 虽然语法不同,但都支持通过链式操作组合多个函数:
pythonCopy Code
# Python(需转换为列表或迭代器) numbers = [1,2,3] result = list(map(lambda x: x+1, filter(lambda x: x>1, numbers))) # [3,4]
javascriptCopy Code
// JavaScript [1, 2, 3].filter(x => x > 1).map(x => x + 1); // [3,4]
6. 元素遍历逻辑一致
- 所有函数均按顺序遍历集合元素,且不对原数据产生副作用(返回新集合)。
总结
两者在功能设计、参数结构和函数式编程思想上高度一致,主要差异在于语法细节(如Python的独立函数 vs. JavaScript的数组方法)和返回类型(迭代器 vs. 数组)。熟悉其中一种语言后,可以快速迁移到另一种的使用。
Python vs Javascript Map Filter Reduce 对比_python 类似javascript的reduce方法-CSDN博客