dimanche 11 février 2018

How to get every possible list of substrings from a string in Python?

i'm trying to turn a given string ("hello") to a list containing every list of substrings. for example: [["hello"],["h,"ello"],["he","llo"],["hel","lo"],["hell","o"],["h","e","llo"],["h","e","l","lo"],["h","e","l","l","o],["he","l,"lo"],["hel","l,"o"],["hell","o]....etc....]. i understand the fastest way should be a recursion function, but i just can't get it right. something similar to:

x = "hello"
wordset=[]
string_div(0,x,wordset)
...
...
def string_div(i,word,wordset)
  wordset.append(wordset+[word[i:])
  ......
  

help would be appreciated! thanks

Aucun commentaire:

Enregistrer un commentaire