I want to get combinations of substrings like an example below:
- original string: “This is my pen”
- expected output list: [“This is my pen”, “This is mypen”, “This ismy pen”, “Thisis my pen”, “This ismypen”, “Thisismy pen”, “Thisismypen”]
As you can see in the example, I’d like to get all substring combinations by removing a white space character(s) while keeping the order of the sequence.
I’ve tried to use strip(idx)
function and from itertools import combinations
. But it was hard to keep the order of the original sentence and also get all possible cases at the same time.
Any basic ideas will be welcomed! Thank you very much.
- I’m a newbie to programming so please let me know if I need to write more details. Thanks a lot.
CodePudding user response:
Try this:
import itertools
s = "This is my pen"
s_list = s.split()
s_len = len(s_list)
Then:
r = ["".join(itertools.chain(*zip(s_list, v ("",))))
for v in itertools.product([" ", ""], repeat=s_len-1)]
This results in r
having the following value:
['This is my pen',
'This is mypen',
'This ismy pen',
'This ismypen',
'Thisis my pen',
'Thisis mypen',
'Thisismy pen',
'Thisismypen']
If you just want to iterate over the values, you can avoid creating the top-level list as follows:
for u in ("".join(itertools.chain(*zip(s_list, v ("",))))
for v in itertools.product([" ", ""], repeat=s_len-1)):
print(u)
which produces:
This is my pen
This is mypen
This ismy pen
This ismypen
Thisis my pen
Thisis mypen
Thisismy pen
Thisismypen