Oct-14-2018, 04:23 AM
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:
In effect, that line replaces this in the original:
d[c] = d.get(c, 0) + 1In 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