在Python中为BMI计算创建for循环

2024-06-10 15:36:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我在为问题创建BMI循环时遇到问题

以下是个人资料:

[['John', 84.9, 184], ['Ryan', 81.8, 177], ['Bobby', 86.1, 190], ['Lambda Llama', 140, 180], ['Lambda Llama', 140, 180], ['Lambda Llama', 140, 180], ['Pete', 92.2, 188], ['Esther', 69.6, 159], ['Jane', 72.0, 166], ['Samantha', 51.3, 162], ['Samantha', 51.3, 162]]

问题:

使用您在上面创建的列表(person_数据)计算每个人的BMI

公式如下:

𝑊𝑒𝑖𝑔ℎ𝑡(𝑘𝑔)/𝐻𝑒𝑖𝑔ℎ𝑡(𝑚)^2=𝐵𝑀𝐼

提示:您将迭代一个索引;其他两个索引将保持不变,即您始终需要每个嵌套列表的第一个(高度)和第二个(重量)元素

附加提示:请确保检查输入身高测量值的单位。BMI公式使用米(m),您的身高单位为厘米(cm)。一米中有100厘米;要从厘米转换为米,请除以100

# Write your BMI calculation loop here
for names in person_data:
    weight = person[1]
    height = person[2] % 100
    bmi = weight % height**2
    person.append(bmi)

# Inside the loop print out each value for the bmi 
print(bmi)

所以当我打印体重指数时,它只会给我一个数字(51.3)。我如何得到每个人的体重指数来打印

任何指导都将不胜感激!我仍处于学习的初级阶段

还有一个额外的目标,就是如何通过列表理解来做到这一点……关于这看起来像什么,还有什么指导吗

#  Calculate the BMI using a list comprehension

# One way (suggestion)


# More Pythonic way (suggestion)

Tags: thelambdaloop列表for单位公式person
2条回答

您将namesperson混合

pList = [['John', 84.9, 184], ['Ryan', 81.8, 177], ['Bobby', 86.1, 190], ['Lambda Llama', 140, 180], ['Lambda Llama', 140, 180], ['Lambda Llama', 140, 180], ['Pete', 92.2, 188], ['Esther', 69.6, 159], ['Jane', 72.0, 166], ['Samantha', 51.3, 162], ['Samantha', 51.3, 162]]
# Write your BMI calculation loop here

for person in pList:
    weight = person[1]
    height = person[2] % 100
    bmi = weight % height**2
    person.append(bmi)


# Inside the loop print out each value for the bmi 
print(pList)

输出

[['John', 84.9, 184, 84.9], ['Ryan', 81.8, 177, 81.8], ['Bobby', 86.1, 190, 86.1], ['Lambda Llama', 140, 180, 140], ['Lambda Llama', 140, 180, 140], ['Lambda Llama', 140, 180, 140], ['Pete', 92.2, 188, 92.2], ['Esther', 69.6, 159, 69.6], ['Jane', 72.0, 166, 72.0], ['Samantha', 51.3, 162, 51.3], ['Samantha', 51.3, 162, 51.3]]

很快,您就走上了正确的道路,但是在python中“%”表示模。 这与您所期望的divide不同

转到python部分。 一种简单的方法叫做列表理解


persons = [['John', 84.9, 184], ['Ryan', 81.8, 177], ['Bobby', 86.1, 190], ['Lambda Llama', 140, 180],
           ['Lambda Llama', 140, 180], ['Lambda Llama', 140, 180], ['Pete', 92.2, 188], ['Esther', 69.6, 159],
           ['Jane', 72.0, 166], ['Samantha', 51.3, 162], ['Samantha', 51.3, 162]]

if __name__ == '__main__':

    bmis = [p[1] / (p[2] / 100) ** 2 for p in persons]
    print(bmis)
    [person.append(bmi) for bmi, person in zip(bmis, persons)]
    print(persons)

然而,我喜欢在python中使用函数式编程(它并没有使阅读变得更容易,只是使它只需要一行代码)

persons = [['John', 84.9, 184], ['Ryan', 81.8, 177], ['Bobby', 86.1, 190], ['Lambda Llama', 140, 180],
           ['Lambda Llama', 140, 180], ['Lambda Llama', 140, 180], ['Pete', 92.2, 188], ['Esther', 69.6, 159],
           ['Jane', 72.0, 166], ['Samantha', 51.3, 162], ['Samantha', 51.3, 162]]

if __name__ == '__main__':
    list(map(lambda e, q: e.append(q), persons, map(lambda p: p[1] / (p[2] / 100) ** 2, persons)))

两种方法给出了完全相同的答案:

>>>
[['John', 84.9, 184, 25.07679584120983],
 ['Ryan', 81.8, 177, 26.109993935331477],
 ['Bobby', 86.1, 190, 23.850415512465375],
 ['Lambda Llama', 140, 180, 43.20987654320987],
 ['Lambda Llama', 140, 180, 43.20987654320987],
 ['Lambda Llama', 140, 180, 43.20987654320987],
 ['Pete', 92.2, 188, 26.086464463558173],
 ['Esther', 69.6, 159, 27.530556544440483],
 ['Jane', 72.0, 166, 26.1286108288576],
 ['Samantha', 51.3, 162, 19.547325102880652],
 ['Samantha', 51.3, 162, 19.547325102880652]]

另一方面,在标准库中有一个很棒的函数名为pprint,这就是我如何很好地打印出该数组。它以一种很好的方式为您自动格式化数据。要使用它:

import pprint

persons = [[...]]

pprint.pprint(persons)

相关问题 更多 >