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/string: how to get all the possible combinations in string considering only space for splitting the string

I have a string
dt = '301 302 303'
how can I get different combinations of above string with considering only spaces while splitting the string.

# output
301
302
303
301 302
301 303
302 303
301 302 303

>Solution :

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

Please use itertools module
turn the string to a list
and then iterate

dt = '301 302 303' 
import itertools
list1 = dt.split()
for i in range(1,len(list1) + 1):
    for subset in itertools.combinations(list1,i):
        print(subset)
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