Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Python中的映射和过滤方法_Javascript_Python_Python 3.x - Fatal编程技术网

Javascript Python中的映射和过滤方法

Javascript Python中的映射和过滤方法,javascript,python,python-3.x,Javascript,Python,Python 3.x,我有一段代码,它使用.map()和.filter()将bmi值附加到此数组中的对象。我现在正在学习python,我想知道这段代码在python中有什么不同。具体地说,当我在python中这样做时,我无法向键添加额外的值 let people=[{ 姓名:“艾米”, 体重:152磅, 身高:63英寸 }, { 姓名:“乔”, 体重:120磅, 身高:64英寸 }, { 姓名:“汤姆”, 体重:210磅, 身高:78英寸 }, { 姓名:“吉姆”, 体重:180磅, 身高:68英寸 }, { 姓名

我有一段代码,它使用.map()和.filter()将bmi值附加到此数组中的对象。我现在正在学习python,我想知道这段代码在python中有什么不同。具体地说,当我在python中这样做时,我无法向键添加额外的值

let people=[{
姓名:“艾米”,
体重:152磅,
身高:63英寸
},
{
姓名:“乔”,
体重:120磅,
身高:64英寸
},
{
姓名:“汤姆”,
体重:210磅,
身高:78英寸
},
{
姓名:“吉姆”,
体重:180磅,
身高:68英寸
},
{
姓名:“珍”,
体重:120磅,
身高:62英寸
},
{
姓名:“安”,
体重:252磅,
身高:63英寸
},
{
姓名:“本”,
体重:240磅,
身高:72英寸
},
];
让Poundstockg=(重量)=>重量/2.205;
让英寸流量计=(高度)=>高度/39.37;
让addbmi=(person)=>{
person.bmi=poundstockg(person.pounds\u-weight)/Math.pow(英寸米(person.inches\u-height),2;
返回人;
}
isOverweight=person=>person.bmi>=25&&person.bmi<30;
isObese=person=>person.bmi>=30;
让超重的人=人.map(addbmi).filter(isOverweight);
让肥胖的人=人.map(addbmi).filter(isObese);
人民地图(addbmi);
控制台日志(超重的人);
控制台日志(“”);

console.log(肥胖者)这里有一个到Python的翻译,它与JavaScript风格非常接近

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)

你的python代码在哪里?你不应该像
let\u People=People.map(addbmi)
这样做,至少不要用python。i、 例如,不要将功能性构造与副作用混为一谈,但你可以轻松创建一些等效的东西,但你必须提供你正在使用的不起作用的代码。好的,谢谢你让我知道。
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}]