[Tutor] python lists/nested lists

Alan Gauld alan.gauld at btinternet.com
Sat Apr 13 23:30:20 CEST 2013


On 13/04/13 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?

This sounds like homework which we don;t do for you.

However to calculate the average take the sum and divide by the count.
Do you know how to do those two things?

Once you have the average you need to iterate over your list and 
subtract the average you calculated (and stored) and print the result.

> 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]

I don't think that's really the structure you intended?
but if it is there is a bunch of missing brackets at the end...

> and I would like to print out all the patients with a temp > 100 how would I do so?

You would iterate over the list testing the temperature element of each 
sublist against 100 and if necessary printing the result.

Now try writing some code and if it doesn't work show us what you've 
done including any error messages(in full) that you get.

PS. As a matter of style your patient records should probably be tuples 
or dictionaries rather than lists. But that's a nicety to keep for 
another time...

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list