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
#1
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.
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
Reply
#4
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#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
#6
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.
Reply
#7
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
Reply
#8
I changed the thread title for you
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
(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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

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