[Tutor] python lists/nested lists

Mark Lawrence breamoreboy at yahoo.co.uk
Sat Apr 13 23:16:09 CEST 2013


On 13/04/2013 21:53, Soliman, Yasmin wrote:
> How can I calculate the average of a list of numbers (eg [2,5,8,7,3] ) and then subtract the avg from the original numbers in the list and print?
>

lst = [2,5,8,7,3]
avg = sum(lst) / len(lst)
print(avg)
for n in lst:
     print(n - avg)

> Also, if I have a nested list:
> sick_patients=[['Sam', 'M', 65, 'chest pain', 101.6], [['Sarah', 'F', 73, 'dizziness', 98.6], [['Susie', 'F', 34, 'headache', 99.7], [['Scott', 'M', 12, 'stom ache', 102.3]
>
> and I would like to print out all the patients with a temp > 100 how would I do so?

for patient in sick_patients:
     if patient[-1] > 100:
         print(patient)

The above works if you fix the syntax errors in sick_patients, you do't 
need all the double brackets.

-- 
If you're using GoogleCrap™ please read this 
http://wiki.python.org/moin/GoogleGroupsPython.

Mark Lawrence



More information about the Tutor mailing list