Python Keep new words in dictionary by using key as prefix
I have a dictionary say,
stringToListDict = {'foo' : [], 'bar' : []}
Now lets say we do
+foofoo
stringToListDict = {'foo' : ['foofoo'], 'bar' : []}
+barbar
stringToListDict = {'foo' : ['foofoo'], 'bar' : ['barbar']}
+foobarbar
stringToListDict = {'foo' : ['foofoo', 'foobarbar'], 'bar' : ['barbar']}
+notMatchingAnyKey
Simply discard this new string.
As you can see the added string goes by matching keys as prefix.
I can do this by traversing through each key one by one of the dictionary till I get a matching prefix. But is there any other elegant or efficient approach? You don't have to worry of edge scenarios like what would happen if :
stringToListDict = {'foo' : ['foofoo'], 'foobar' : [], 'bar' : ['barbar']}
then +foobarbar
FYI, this is not an assignment.
stringToListDict = {'foo' : [], 'bar' : []}
Now lets say we do
+foofoo
stringToListDict = {'foo' : ['foofoo'], 'bar' : []}
+barbar
stringToListDict = {'foo' : ['foofoo'], 'bar' : ['barbar']}
+foobarbar
stringToListDict = {'foo' : ['foofoo', 'foobarbar'], 'bar' : ['barbar']}
+notMatchingAnyKey
Simply discard this new string.
As you can see the added string goes by matching keys as prefix.
I can do this by traversing through each key one by one of the dictionary till I get a matching prefix. But is there any other elegant or efficient approach? You don't have to worry of edge scenarios like what would happen if :
stringToListDict = {'foo' : ['foofoo'], 'foobar' : [], 'bar' : ['barbar']}
then +foobarbar
FYI, this is not an assignment.
Комментарии
Отправить комментарий