average of values in dict with use of for - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: average of values in dict with use of for (/thread-26322.html) |
average of values in dict with use of for - Daromir - Apr-28-2020 Hello, Newbie here, so im sorry for all mistakes related to posting this. I recently went through Udemy "Complete Python Course from Basics to Brilliance in HD" and now im looking for ways to hone my knowledge. Was going through some homework tests from a friend and wondered if i could get help in finishing this one: dic={"123/2018":8,"124/2017":6,"125/2018":7,"126/2017":9} del dic["124/2017"] dic["435/2018"]="6" print(dic)What test asks now is to print average of values in dic with use of for. All suggestions/comments are welcome. If it is not violating any rule, i would expand this thread with my future problems. Thank you all in advance. RE: Thread opened by a newbie - buran - Apr-28-2020 please, show what have you tried in using for loop to iterate over dict values. if you need you can check out tutorials section: https://python-forum.io/Thread-for-loops https://python-forum.io/Thread-Basic-Dictionaries RE: Thread opened by a newbie - deanhystad - Apr-28-2020 https://docs.python.org/3/tutorial/datastructures.html#dictionaries Look under 5.6 Looping Techniques And use better titles for your threads. "I'm a helpless newbie" type topics just make me want to ignore the post. RE: Thread opened by a newbie - perfringo - Apr-28-2020 As this is homework I will provide ways without for-loop. This may or may not give an ideas. >>> d = {'ham': 7, 'spam': 42} >>> sum(d.values()) / len(d.values()) 24.5There is mean() in statistics module, so one can do: >>> from statistics import mean >>> mean(d.values()) 24.5 RE: Thread opened by a newbie - Daromir - Apr-28-2020 (Apr-28-2020, 01:24 PM)perfringo Wrote: As this is homework I will provide ways without for-loop. This may or may not give an ideas. Something like this came to my mind first, but i cant figure out working way that involves for, since that is what is asked for. (Apr-28-2020, 10:56 AM)buran Wrote: please, show what have you tried in using for loop to iterate over dict values. (Apr-28-2020, 12:33 PM)deanhystad Wrote: https://docs.python.org/3/tutorial/datastructures.html#dictionaries I guess i failed to find what i need in your links, but that stuff will come in handy for sure. Is there a way i can change title thread? Current state of things dic={"123/2018":8,"124/2017":6,"125/2018":7,"126/2017":9} del dic["124/2017"] #ili recnik.pop("124/2017") dic["435/2018"]=6 print(dic) med=sum(dic.values())/len(dic) print(med) RE: Thread opened by a newbie - deanhystad - Apr-28-2020 Your original post says "What test asks now is to print average of values in dic with use of for." Is the for loop a requirement? If so, you solution is not correct. RE: Thread opened by a newbie - Daromir - Apr-28-2020 Last post was sort of a test to see if i can comunicate properly on this forum. Which i guess i can not edit now. dic={"123/2018":8,"124/2017":6,"125/2018":7,"126/2017":9} del dic["124/2017"] #ili dic.pop("124/2017") dic["435/2018"]=6 print(dic) med=0 for i in dic.values(): med+=i med=med/len(dic) #med=sum(dic.values())/len(dic) print(med)Solved by a friend RE: average of values in dict with use of for - buran - Apr-28-2020 I changed the thread title for you RE: Thread opened by a newbie - perfringo - Apr-29-2020 (Apr-28-2020, 05:09 PM)Daromir Wrote: Something like this came to my mind first, but i cant figure out working way that involves for, since that is what is asked for. My personal subjective opinion is that for-loop is redundant, but it can be easily accommodated into code: >>> d = {'ham': 7, 'spam': 42} >>> sum(i for i in d.values()) / len(d.values()) 24.5 |