首页 > 解决方案 > 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);

标签: javascriptpythonpython-3.x

解决方案


这是一个接近 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}]

推荐阅读