Python vs Javascript Map Filter Reduce 对比

概念

在这里插入图片描述

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值