javascript - Python 中的 Map 和 Filter 方法
问题描述
我有这段代码,它使用 .map() 和 .filter() 将 bmi 值附加到此数组中的对象。我现在正在学习 python,我想知道这个片段在 python 中会有什么不同。具体来说,当我在 python 中执行此操作时,我无法向键添加其他值。
let people = [{
name: "Amy",
pounds_weight: 152,
inches_height: 63
},
{
name: "Joe",
pounds_weight: 120,
inches_height: 64
},
{
name: "Tom",
pounds_weight: 210,
inches_height: 78
},
{
name: "Jim",
pounds_weight: 180,
inches_height: 68
},
{
name: "Jen",
pounds_weight: 120,
inches_height: 62
},
{
name: "Ann",
pounds_weight: 252,
inches_height: 63
},
{
name: "Ben",
pounds_weight: 240,
inches_height: 72
},
];
let poundsToKg = (weight) => weight / 2.205;
let inchesToMeters = (height) => height / 39.37;
let addbmi = (person) => {
person.bmi = poundsToKg(person.pounds_weight) / Math.pow(inchesToMeters(person.inches_height), 2);
return person;
}
isOverweight = person => person.bmi >= 25 && person.bmi < 30;
isObese = person => person.bmi >= 30;
let overweight_People = people.map(addbmi).filter(isOverweight);
let obese_People = people.map(addbmi).filter(isObese);
people.map(addbmi);
console.log(overweight_People);
console.log(" ");
console.log(obese_People);
解决方案
这是一个接近 JavaScript 风格的 Python 翻译
import math
import pprint
pp = pprint.PrettyPrinter(indent=4) # Used for pretty-printing output
# Using Python dictionaries and lists to replace JavaScript objects
# string Keys for Python dictionaries must be placed in single or double quotes
people = [{
"name": "Amy",
"pounds_weight": 152,
"inches_height":63
},
{
"name": "Joe",
"pounds_weight": 120,
"inches_height":64
},
{
"name": "Tom",
"pounds_weight": 210,
"inches_height":78
},
{
"name": "Jim",
"pounds_weight": 180,
"inches_height":68
},
{
"name": "Jen",
"pounds_weight": 120,
"inches_height":62
},
{
"name": "Ann",
"pounds_weight": 252,
"inches_height":63
},
{
"name": "Ben",
"pounds_weight": 240,
"inches_height":72
},
]
# Python Lambda expression replacing JavaScript's
poundsToKg = lambda weight: weight / 2.205
inchesToMeters = lambda height: height / 39.37
# Python Function
def addbmi(person):
" Adds bmi to person "
person["bmi"] = poundsToKg(person["pounds_weight"]) / math.pow(inchesToMeters(person["inches_height"]), 2)
return person
# More Python lambda expressions replacing JavaScript's
isOverweight = lambda person: person["bmi"] >= 25 and person["bmi"] < 30
isObese = lambda person: person["bmi"] >= 30
overweight_people = lambda people: filter(isOverweight, map(addbmi, people))
obese_people = lambda people: filter(isObese, map(addbmi, people))
print('Overweight')
overweight = list(overweight_people(people))
pp.pprint(overweight)
print('Obese')
obese = list(obese_people(people))
pp.pprint(obese)
输出
Overweight
[ { 'bmi': 26.92059936160573,
'inches_height': 63,
'name': 'Amy',
'pounds_weight': 152},
{ 'bmi': 27.363832003389586,
'inches_height': 68,
'name': 'Jim',
'pounds_weight': 180}]
Obese
[ { 'bmi': 44.631519994241074,
'inches_height': 63,
'name': 'Ann',
'pounds_weight': 252},
{ 'bmi': 32.54381666246746,
'inches_height': 72,
'name': 'Ben',
'pounds_weight': 240}]
推荐阅读
- swift - 根据拾取的图像(来自图像选择器)框架更改图像视图框架 SWIFT
- python - loss: nan 何时建立共享单车模型
- python - 谷歌运筹学工具中的目标函数“公平”系数
- javascript - 面向对象编程和调用函数
- python - 如何使用“请求”模块在网站内进行搜索?
- c++ - 制作模板化节点类时重新定义错误
- javascript - GraphQL 和 mongoose 在验证用户时返回 nulll
- sql-server - 用于检索每个用户的总订单数的 SQL 查询
- excel - Excel VBA:如何确定当前运行时用户窗体中最后使用的文本框或组合框?
- excel - 如何在 Excel 中将文本更改为数字?