Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python get combination of continous words

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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']]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading