I have an array of n words as strings such as:
input: ["just", "a", "test"]
What I need to do is create all possible combinations of these words separated by spaces as well as in combination with the original strings. For example, the above should create:
output: [["just", "a", "test"], ["just a", "test"], ["just a test"], ["just", "a test"]]
I've been using itertools but can't get it to do what I need. What I have at the moment:
iterable = ['just', 'a', 'test']
for n in chain.from_iterable(combinations(iterable, n) for n in range(len(iterable)+1)):
print(n)
The following almost works as required:
iterable = ['just', 'a', 'test']
L = [''.join(reversed(x)).rstrip()
for x in product(*[(c, c+' ') for c in reversed(iterable)])]
print(L)
Thank you.
EDIT:
To clarify how this should work for an array of length 4: input: ['an', 'even', 'bigger', 'test']`
output:
['an', 'even', 'bigger', 'test']
['an even', 'bigger', 'test']
['an even bigger', 'test']
['an even bigger test']
['an', 'even bigger', 'test']
['an even', 'bigger test']
['an', 'even bigger test']
['an', 'even', 'bigger test']