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?
#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


Messages In This Thread
RE: how do i use get for my histogram function? - by stullis - Oct-14-2018, 04:23 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Need help with NumPy Histogram function coding Triikey 1 972 May-15-2023, 01:45 PM
Last Post: deanhystad
  Plotting histogram of dataframe column Mark17 4 2,685 Jul-30-2020, 09:52 AM
Last Post: Mark17
  Can someone find out what kind of histogram is it? J_tin 1 1,826 Apr-26-2020, 05:23 PM
Last Post: buran
  typeerror, building histogram from data newatpython11 7 3,835 Jul-17-2019, 12:54 PM
Last Post: ichabod801
  Histogram using pandas dataframe not showing proper output ift38375 1 2,227 Jul-04-2019, 10:43 PM
Last Post: scidam
  Histogram and text file. pawlo392 1 4,164 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