Python Forum
average of values in dict with use of for
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
average of values in dict with use of for
#5
(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.

>>> d = {'ham': 7, 'spam': 42}
>>> sum(d.values()) / len(d.values())
24.5
There is mean() in statistics module, so one can do:

>>> from statistics import mean
>>> mean(d.values())
24.5

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.
if you need you can check out tutorials section:
https://python-forum.io/Thread-for-loops
https://python-forum.io/Thread-Basic-Dictionaries

(Apr-28-2020, 12:33 PM)deanhystad Wrote: https://docs.python.org/3/tutorial/datas...ctionaries

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.

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)
Reply


Messages In This Thread
RE: Thread opened by a newbie - by buran - Apr-28-2020, 10:56 AM
RE: Thread opened by a newbie - by deanhystad - Apr-28-2020, 12:33 PM
RE: Thread opened by a newbie - by perfringo - Apr-28-2020, 01:24 PM
RE: Thread opened by a newbie - by Daromir - Apr-28-2020, 05:09 PM
RE: Thread opened by a newbie - by perfringo - Apr-29-2020, 05:26 AM
RE: Thread opened by a newbie - by deanhystad - Apr-28-2020, 05:52 PM
RE: Thread opened by a newbie - by Daromir - Apr-28-2020, 06:00 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020