Thanks Thomas! To reply the subject's question: I don't have to. The following works perfectly.
def populate_trie(trie, sequence, position=None): """ Populate a trie. Assume that the counter is always at `position` 0 while the `position` of the dictionary is the last one. >>> trie = {} >>> populate_trie(trie, 'taspython') {'t': {'a': {'s': {'p': {'y': {'t': {'h': {'o': {'n': {}}}}}}}}}} >>> trie = {} >>> populate_trie(trie, 'kalo', 1) {'k': [1, {'a': [1, {'l': [1, {'o': [1, {}]}]}]}]} >>> trie = {} >>> populate_trie(trie, 'heh', 2) {'h': [1, 0, {'e': [1, 0, {'h': [1, 0, {}]}]}]} >>> trie = {} >>> trie = populate_trie(trie, 'heh', 1) >>> populate_trie(trie, 'hah', 1) {'h': [2, {'a': [1, {'h': [1, {}]}], 'e': [1, {'h': [1, {}]}]}]} """ if (position is not None) and (position >= 1): embedded_obj = [0] * position embedded_obj.append({}) else: embedded_obj = {} d2 = trie for i, character in enumerate(sequence): d2 = access_trie(trie, sequence[:i], position) if character not in d2: if position is None: d2[character] = deepcopy(embedded_obj) else: d2[character] = d2.get(character, deepcopy(embedded_obj)) d2[character][0] += 1 elif position is not None: d2[character][0] += 1 return trie Best regards, Dimitris Leventeas -- Dimitris Leventeas http://students.ceid.upatras.gr/~lebenteas/ -- http://mail.python.org/mailman/listinfo/python-list