Search code examples
pythonlistcombinationspython-itertools

Python get combination of continous words


I have a list like this:

tokens = ["hi", "how", "are", "you"]

I am trying to get combinations 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 provides combinations of everything not only the next words. Waht is wrong here?


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']]