-1

I looked at this code:

>>> _end = '_end_'
>>> 
>>> def make_trie(*words):
...     root = dict()
...     for word in words:
...         current_dict = root
...         for letter in word:
...             current_dict = current_dict.setdefault(letter, {})
...         current_dict = current_dict.setdefault(_end, _end)
...     return root
... 
>>> make_trie('foo', 'bar', 'baz', 'barz')
{'b': {'a': {'r': {'_end_': '_end_', 'z': {'_end_': '_end_'}}, 
         'z': {'_end_': '_end_'}}}, 
'f': {'o': {'o': {'_end_': '_end_'}}}}

from this link: How to create a TRIE in Python, but I don't quite understand why does the author create the temporary variable current_dict since you are always just editing the dictionary called root...

Community
  • 1
  • 1
Kevin Zhao
  • 2,113
  • 2
  • 14
  • 18
  • I deleted my totally wrong thought about how the code works, sorry! I still do think this question belongs at codereview.stackexchange.com. – GreenAsJade May 17 '15 at 01:16
  • Should I ask questions about implementations there in the future? if so, why? – Kevin Zhao May 18 '15 at 16:16
  • StackOverflow is for specific questions about a specific problem with code (read [help] for more details). CodeReview is for soliciting input about how code can be improved and the rationale for doing things in certain ways. – GreenAsJade May 18 '15 at 16:19
  • I see, thanks! I just added my next question on unionfind to codereview here: http://codereview.stackexchange.com/questions/91070/union-find-why-use-value-not-idp – Kevin Zhao May 18 '15 at 16:24

1 Answers1

2

I don't quite understand why does the author create the temporary variable current_dict since you are always just editing the dictionary called root...

No, you're not. If it was always editing the root dictionary, the result would be very different. Every time the assignment inside the loop is executed:

...         current_dict = root
...         for letter in word:
...             current_dict = current_dict.setdefault(letter, {}) # this one

current_dict is set to a dictionary one level further in. This loop traverses the trie, using setdefault to build missing parts as needed. We have to assign the setdefault result to current_dict to keep going down the trie instead of staying at top level, and we have to use a separate current_dict variable instead of root so we can assign current_dict = root to get back to top level once we're done with a word.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • why is it that `current_dict` is set to a dictionary one level further in? I don't see how that happened. – Kevin Zhao May 17 '15 at 18:04
  • @KevinZhao: Do you know what `setdefault` returns? – user2357112 May 17 '15 at 18:34
  • According to TutorialsPoint(http://www.tutorialspoint.com/python/dictionary_setdefault.htm), "This method returns the key value available in the dictionary and if given key is not available then it will return provided default value." so basically the current_dict will equal to a new dictionary after the first time, then when I run the setdefault line again, I am setting the new dictionary's key and values, which is one level in, is that right? – Kevin Zhao May 18 '15 at 16:13
  • @KevinZhao: Correct. – user2357112 May 18 '15 at 18:32