Python Forum
how do i use get for my histogram function?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how do i use get for my histogram function?
#1
def histogram(s):
    d = dict()
    for c in s:
        if c not in d:
            d[c] = 1
        else:
            d[c] += 1
    return d

h = histogram('brontosaurus')
print(h)
Output:
{'b': 1, 'r': 2, 'o': 2, 'n': 1, 't': 1, 's': 2, 'a': 1, 'u': 2}
Dictionaries have a method called get that takes a key and a default value. If the key
appears in the dictionary, get returns the corresponding value; otherwise, it returns the
default value. For example:
>>> h = histogram('a')
>>> h
{'a': 1}
>>> h.get('a', 0)
1
>>> h.get('b', 0)
0
As an exercise, use get to write histogram more concisely. You should be able to eliminate
the if statement.

I don't understand get and that's where I'm stuck
Reply
#2
get is for java
in python use:
value = your_dict[your_key]
Reply
#3
(Oct-13-2018, 05:46 PM)Larz60+ Wrote: get is for java in python use:
value = your_dict[your_key]
Larz, OP means
dic.get(key[,defualt]) method

@OP: what it does is to return default value if key is not present in the dict. If you don't use it, in this case you will get KeyError. So it's a nice way to return default value instead of getting error.
Now, think how you can replace the if statement using dict.get()
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
#4
def histogram(s):
    d = dict()
    for c in s:
        d[c] = d.get(c, 0) + 1
    return d
h = histogram('brontosaurus')
print(h)
{'b': 1, 'r': 2, 'o': 2, 'n': 1, 't': 1, 's': 2, 'a': 1, 'u': 2}
I don't get what I exactly did there but it worked.
Reply
#5
The dict.get() method does value retrieval from the dict (just like dict[key]) with exception checking added. In the event that the key is not in the dict, dict.get() returns the default value you provide. On line 4, you used:

d[c] = d.get(c, 0) + 1
In your code, dict.get() searches dict "d" for key "c". If "c" is not found, it returns the default of 0. If "c" is found, it returns the value of "c" in the dict. In either case, it then adds 1 to the value returned by dict.get() and assigns the value of "c" in the dict to the new value.

In effect, that line replaces this in the original:

if c not in d:
    d[c] = 1
else:
    d[c] += 1
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Need help with NumPy Histogram function coding Triikey 1 944 May-15-2023, 01:45 PM
Last Post: deanhystad
  Plotting histogram of dataframe column Mark17 4 2,649 Jul-30-2020, 09:52 AM
Last Post: Mark17
  Can someone find out what kind of histogram is it? J_tin 1 1,801 Apr-26-2020, 05:23 PM
Last Post: buran
  typeerror, building histogram from data newatpython11 7 3,794 Jul-17-2019, 12:54 PM
Last Post: ichabod801
  Histogram using pandas dataframe not showing proper output ift38375 1 2,196 Jul-04-2019, 10:43 PM
Last Post: scidam
  Histogram and text file. pawlo392 1 4,134 May-24-2019, 03:14 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

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