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

Creating a for loop for BMI calculation in Python

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

这里是person_data:

[['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_data) 计算每个人的 BMI。

公式如下:

ℎ()/ℎ()^2=

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

附加提示:确保检查输入的高度测量值的单位。 BMI 公式使用米 (m),您的身高以厘米 (cm) 为单位。一米有100厘米;从 cm 转换为 m,除以 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)

所以当我打印 BMI 时,它只给了我一个数字 (51.3)。我如何得到每个人的 BMI 来打印?

任何方向将不胜感激!我还在学习的初级阶段..

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

#  Calculate the BMI using a list comprehension

# One way (suggestion)


# More Pythonic way (suggestion)

您将 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 中的“%”表示取模。 这与您期望的除法不同。

进入 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)