欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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'...

概念

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

本文地址:https://blog.csdn.net/fimtestxyz/article/details/109255223

相关标签: javascript python