+ 5
2.2 Practice - Vaccinations Report - Python for Data Science
We have a report on the number of flu vaccinations in a class of 20 people. It has the following numbers: never: 5 once: 8 twice: 4 3 times: 3 What is the mean number of times those people have been vaccinated? Output the result using the print() statement Hint: Think about the data this way: it contains 20 values, each representing the number of vaccinations the corresponding person had. === This is introductory but I can't figure out the solution. Could you please help me? Neither line seems to work for the hidden case: print( (5+8+4+3) / 4 ) print( (5+8+4+3) / 20 )
7 Respuestas
+ 3
Thanks Jerry Hobby, good point. I tried this but it doesn't work either:
print( (0*5) + (1*8) + (2*4) + (3*3) / 4 )
What am I missing to find the mean of the number of flu vaccinations for 20 people?
+ 2
Yes you can try making a dict of values and append the values to the are and find the avg👌
Something like
nums={0:5,1:8,2:4,3:3}
# since 5 is once,8 is twice and so on..
arr=empty list
for key,val in nums.items():
for i in range(key):
append values to arr
#arr will be equal to [8,4,4,3,3,3]
#mean
print(sum(arr)/20)
+ 2
[1] vac_nums = [0,0,0,0,0,
1,1,1,1,1,1,1,1,
2,2,2,2,
3,3,3]
[2] [vac_nums.count(x) * x for x in set(vac_nums)]
>>> [0, 8, 8, 9]
[3] len(vac_nums)
>>>> 20
[4] sum([vac_nums.count(x) * x for x in set(vac_nums)]) / len(vac_nums)
>>>> 1.25
+ 2
Simply to find the mean, just do the following equation: print((8+8+9)/20)
Since one time vaccination: 8
Since two times vaccination: 4 x 2 = 8
Since three times vaccination: 3 x 3 = 9
Add them all together and then divide by the total number of students which is 20.
This should help.
+ 1
You aren’t adding up twenty numbers. Repeat the numbers the specified number of times.
+ 1
Divide by 20?
0
never=[0,0,0,0,0]
once=[1,1,1,1,1,1,1,1]
twice=[2,2,2,2]
third=[3,3,3]
total=never+once+twice+third
sum(total)/20