This is the list >
person_data = [
['John', 84.9, 184],
['Ryan', 81.8, 177],
['Bobby', 86.1, 190],
['Lambda Llama', 140, 180], # Name, Weights, Heights
['Pete', 92.2, 188],
['Esther', 69.6, 159],
['Jane', 72.0, 166],
['Samantha', 51.3, 162]
]
This is what I can do so far.
BMI = [person_data[0][1]/((person_data[0][2]/100)**2)
for i in person_data]
The result is
[25.07679584120983,
25.07679584120983,
25.07679584120983,
25.07679584120983,
25.07679584120983,
25.07679584120983,
25.07679584120983,
25.07679584120983]
However, I want the list comprehension to loop through all the person's weight and height. Is only doing it for the first nested list. Please help.
You're not using the iteration variable i
anywhere. Each item is a person (list of attributes), so you need to do something like this:
BMI = [person[1]/((person[2]/100)**2) for person in person_data]
Result:
[25.07679584120983,
26.109993935331477,
23.850415512465375,
43.20987654320987,
26.086464463558173,
27.530556544440483,
26.1286108288576,
19.547325102880652]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.