I have a list like this.
tokens = ["hi", "how", "are", "you"]
I am trying to get combination of words upto n=3
my expected output is :
output = [ ["hi"], ["hi", "how" ], ["hi", "how" , "are"], ["how"], ["how", "are"], ["how", "are", "you"], ["are"], ["are", "you"], ["you"]
my code:
comb = []
for i in range(3):
comb += list(itertools.combinations(tokens,i+1))
but my code provide combations of everything not only next words. please help. Thanks
>Solution :
Please check if this code snippet solves your purpose, you can play around with this code:
import itertools
tokens = ["hi", "how", "are", "you"]
output = [tokens[start:end] for start, end in itertools.combinations(range(len(tokens)+1), 2)]
output
Output:
[['hi'], ['hi', 'how'], ['hi', 'how', 'are'], ['hi', 'how', 'are', 'you'], ['how'], ['how', 'are'], ['how', 'are', 'you'], ['are'], ['are', 'you'], ['you']]