python - What is the difference between the solution that uses defaultdict and the one that uses setdefault? -


in think python author introduces defaultdict. following excerpt book regarding defaultdict:

if making dictionary of lists, can write simpler code using defaultdict. in solution exercise 12-2, can http://thinkpython2.com/code/anagram_sets.py, make dictionary maps sorted string of letters list of words can spelled letters. example, 'opst' maps list ['opts', 'post', 'pots', 'spot', 'stop', 'tops']. here’s original code:

 def all_anagrams(filename):      d = {}      line in open(filename):          word = line.strip().lower()          t = signature(word)          if t not in d:              d[t] = [word]          else:              d[t].append(word) return d 

this can simplified using setdefault, might have used in exercise 11-2:

 def all_anagrams(filename):      d = {}      line in open(filename):          word = line.strip().lower()          t = signature(word)          d.setdefault(t, []).append(word)       return d 

this solution has drawback makes new list every time, regardless of whether needed. lists, that’s no big deal, if factory function complicated, might be. can avoid problem , simplify code using defaultdict:

def all_anagrams(filename):     d = defaultdict(list)     line in open(filename):         word = line.strip().lower()         t = signature(word)         d[t].append(word)     return d 

here's definition of signature function:

def signature(s):     """returns signature of string.      signature string contains of letters in order.      s: string     """     # todo: rewrite using sorted()     t = list(s)     t.sort()     t = ''.join(t)     return t 

what understand regarding second solution setdefault checks whether t (the signature of word) exists key, if not, sets key , sets empty list value, append appends word it. if t exists, setdefault returns value (a list @ least 1 item, string representing word), , append appends word list.

what understand regarding third solution d, represents defaultdict, makes t key , sets empty list value (if t doesn't exist key), word appended list. if t exist, value (the list) returned, , word appended.

what difference between second , third solutions? means code in second solution makes new list every time, regardless of whether it's needed? how setdefault responsible that? how using defaultdict make avoid problem? how second , third solutions different?

the "makes new list every time" means everytime setdefault(t, []) called, new empty list (the [] argument) created default value just in case it's needed. using defaultdict avoids need doing that.

although both solutions return dictionary, 1 using defaultdict returning defaultdict(list) subclass of built-in dict class. not problem. notable effect if print() returned object, output 2 looks quite different.

if don't want whatever reason, can change last statement of function to:

    return dict(d) 

to convert defaultdict(list) created regular dict.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -