dimanche 19 avril 2015

Python Join String to Produce Combinations For All Words in String

If my string is this: 'this is a string', how can I produce all possible combinations by joining each word with its neighboring word?


What this output would look like:



this is a string
thisis a string
thisisa string
thisisastring
this isa string
this isastring
this is astring


What I have tried:



s = 'this is a string'.split()
for i, l in enumerate(s):
''.join(s[0:i])+' '.join(s[i:])


This produces:



'this is a string'
'thisis a string'
'thisisa string'
'thisisastring'


I realize I need to change the s[0:i] part because it's statically anchored at 0 but I don't know how to move to the next word is while still including this in the output.


Aucun commentaire:

Enregistrer un commentaire