How to use .get() in dictionaries

This is the place for queries that don't fit in any of the other categories.

How to use .get() in dictionaries

Postby dboxall123 » Sat Sep 21, 2013 11:55 pm

Hello all. I've recently been looking at some pygame examples by Mekire, and have realized that I have basically no knowledge of dictionaries. So I'm working through a tutorail. In the tutorial the following function has been defined:
Code: Select all
def histogram(s):
   d = dict()
   for letter in s:
      if letter not in d:
         d[letter] = 1
      else:
         d[letter] += 1
   return d
h = histogram('brontosaurus')
print(h)

Simple enough, right? Then it introduced the .get() method, for example
Code: Select all
h.get('a',0)
>>>1
Now it's asked me to use .get() inside the histogram function, and in doing this I should be ablle to eliminate the if statemetn. Initially, I figured this would be easy:
Code: Select all
def hgram(s):
   d = dict()
   for letter in s:
      d[letter] = d.get(letter,1)
   return d
h = hgram('brontosaurus')
print(h)
Of course, this does not work. Now I am stumped. I can't work it out! The worst part is, I bet it's really obvious, and I'm making myself look a fool for not working it out lol! Can anyone give me a hint on this most trivial of problems?
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: How to use .get() in dictionaries

Postby stranac » Sun Sep 22, 2013 11:03 am

This line:
Code: Select all
d[letter] = d.get(letter,1)

gets either the value in the dict or 1, and stores it back in the dict.
Since the only value that can get stored in the dict this way is 1, you'll end up with a bunch of ones.

What you want is increase the value by 1 if it's in the dict, and store a 1 if there's no such key.
HINT: what do you need to increase by 1 to get 1?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 911
Joined: Thu Feb 07, 2013 3:42 pm

Re: How to use .get() in dictionaries

Postby dboxall123 » Sun Sep 22, 2013 1:46 pm

AAAAAARGH! Why can't I work this out?!?!?
What you want is increase the value by 1 if it's in the dict, and store a 1 if there's no such key.
HINT: what do you need to increase by 1 to get 1?
Surely I can only do this with an if statement?
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: How to use .get() in dictionaries

Postby metulburr » Sun Sep 22, 2013 1:47 pm

you would be looking for this line:
Code: Select all
d[letter] = d.get(letter, 0) + 1

You cant set the default to 1, as if the key already exists, there would be no way to increment it. So the answer would be just set it to zero and increment either way.
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1130
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: How to use .get() in dictionaries

Postby dboxall123 » Sun Sep 22, 2013 2:04 pm

Wow.What a dummy I am. Thanks guys!
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 2 guests

cron