Python vs Javascript Map Filter Reduce 对比
程序员文章站
2022-07-02 14:18:02
概念1. MapPythonIn [24]: pilots = [{'id': 10, 'name': 'Poe Dameron', 'years': 14}, ...: {'id': 2, 'name': "Temmin 'Snap' Wexley", 'years': 30}, ...: {'id': 41, 'name': 'Tallissan Lintra', 'years': 16}, ...: {'id': 99, 'name': 'Ello Asty'...
概念
1. Map
- Python
In [24]: pilots = [{'id': 10, 'name': 'Poe Dameron', 'years': 14},
...: {'id': 2, 'name': "Temmin 'Snap' Wexley", 'years': 30},
...: {'id': 41, 'name': 'Tallissan Lintra', 'years': 16},
...: {'id': 99, 'name': 'Ello Asty', 'years': 22}]
In [25]: map(lambda x : f"Mr. {x['name']}", pilots)
Out[25]: <map at 0x22fd24e0108>
In [26]: list(map(lambda x : f"Mr. {x['name']}", pilots))
Out[26]:
['Mr. Poe Dameron',
"Mr. Temmin 'Snap' Wexley",
'Mr. Tallissan Lintra',
'Mr. Ello Asty']
In [27]:
- Javascript
inakamono@ninja MINGW64 ~/wk/coding/hello_node
$ cat map.js
var pilots = [
{
id: 10,
name: "Poe Dameron",
years: 14,
},
{
id: 2,
name: "Temmin 'Snap' Wexley",
years: 30,
},
{
id: 41,
name: "Tallissan Lintra",
years: 16,
},
{
id: 99,
name: "Ello Asty",
years: 22,
}
];
console.log(pilots.map((pilot)=> `Mr. ${pilot.name}`))
inakamono@ninja MINGW64 ~/wk/coding/hello_node
$ node map.js
[
'Mr. Poe Dameron',
"Mr. Temmin 'Snap' Wexley",
'Mr. Tallissan Lintra',
'Mr. Ello Asty'
]
2. Filter
- Python
In [28]: pilots
Out[28]:
[{'id': 10, 'name': 'Poe Dameron', 'years': 14},
{'id': 2, 'name': "Temmin 'Snap' Wexley", 'years': 30},
{'id': 41, 'name': 'Tallissan Lintra', 'years': 16},
{'id': 99, 'name': 'Ello Asty', 'years': 22}]
In [29]: filter(lambda x : x['years'] > 20, pilots)
Out[29]: <filter at 0x22fd24b3188>
In [30]: list(filter(lambda x : x['years'] > 20, pilots))
Out[30]:
[{'id': 2, 'name': "Temmin 'Snap' Wexley", 'years': 30},
{'id': 99, 'name': 'Ello Asty', 'years': 22}]
- Javascript
inakamono@ninja MINGW64 ~/wk/coding/hello_node
$ cat filter.js
var pilots = [
{
id: 10,
name: "Poe Dameron",
years: 14,
},
{
id: 2,
name: "Temmin 'Snap' Wexley",
years: 30,
},
{
id: 41,
name: "Tallissan Lintra",
years: 16,
},
{
id: 99,
name: "Ello Asty",
years: 22,
}
];
console.log(pilots.filter((pilot)=> pilot["years"]> 20 ))
inakamono@ninja MINGW64 ~/wk/coding/hello_node
$ node filter.js
[
{ id: 2, name: "Temmin 'Snap' Wexley", years: 30 },
{ id: 99, name: 'Ello Asty', years: 22 }
]
3. Reduce
- Python
In [20]: import functools
In [21]: pilots = [{'id': 10, 'name': 'Poe Dameron', 'years': 14},
...: {'id': 2, 'name': "Temmin 'Snap' Wexley", 'years': 30},
...: {'id': 41, 'name': 'Tallissan Lintra', 'years': 16},
...: {'id': 99, 'name': 'Ello Asty', 'years': 22}]
In [22]: longestfunctools.reduce(lambda a,b : a if a["years"] > b["years"] else b, pilots)
Out[22]: {'id': 2, 'name': "Temmin 'Snap' Wexley", 'years': 30}
- Javascript
nakamono@ninja MINGW64 ~/wk/coding/hello_node
$ cat reduce.js
var pilots = [
{
id: 10,
name: "Poe Dameron",
years: 14,
},
{
id: 2,
name: "Temmin 'Snap' Wexley",
years: 30,
},
{
id: 41,
name: "Tallissan Lintra",
years: 16,
},
{
id: 99,
name: "Ello Asty",
years: 22,
}
];
longest_year = pilots.reduce((accumulator, pilot)=> {
return accumulator > pilot.years ? accumulator : pilot.years;
},0);
console.log(`Longest year of service=${longest_year}`);
oldest_pilot = pilots.reduce((accumulator, pilot)=> {
return (accumulator.years || 0) > pilot.years ? accumulator : pilot;
},{});
console.log('oldest pilot is as below');
console.log(oldest_pilot);
console.log(oldest_pilot.years);
inakamono@ninja MINGW64 ~/wk/coding/hello_node
$ node reduce.js
Longest year of service=30
oldest pilot is as below
{ id: 2, name: "Temmin 'Snap' Wexley", years: 30 }
30
本文地址:https://blog.csdn.net/fimtestxyz/article/details/109255223
上一篇: 格式化输出、逻辑表达式和字符编码
下一篇: 数仓 DW层 用户活跃度分析主题
推荐阅读
-
Python中的特殊语法:filter、map、reduce、lambda介绍
-
python 内置函数-range()+zip()+sorted()+map()+reduce()+filter()
-
Python map、filter,reduce介绍
-
JavaScript数组迭代方法:forEach(),map(),filter(),reduce(),every(),some(),indexOf(),lastIndexOf(),find()
-
Python中map,reduce,filter和sorted函数的使用方法
-
Python中map,reduce,filter和sorted函数的使用方法
-
Python的lambda表达式、filter、map、reduce等函数的用法
-
Python内置函数之filter map reduce介绍
-
Python中的map、reduce和filter浅析
-
8.Javascript-map、reduce、filter 等高阶函数