recursion

alternate way of doing word split/phrase segmentation in python

alternate way of doing word split/phrase segmentation in python

Doing word split with recursion felt a bit complex to me , so I tried to do it in an easier way. The Hard Way (recursion) -- def word_split(phrase,list_of_words, output = None): if output is None: #Base Case / Initial call output = [] for word in list_of_words: if phrase.startswith(word): output.append(word) return word_split(phrase[len(word):],list_of_words,output) # Recursive Call return output # Result Enter fullscreen mode Exit fullscreen mode gives The Easy Way (indexing/for loop) - def word_split_2(phrase, word_list): output = [] for i in word_list: if i in phrase: output.append(i) return output Enter fullscreen mode Exit fullscreen mode Source link lol
Read More
No widgets found. Go to Widget page and add the widget in Offcanvas Sidebar Widget Area.