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

How to generate substrings of a dot-separated string?

I am struggling to generate a list of substrings from a given list of strings.

I have a list of domains –

domains = ["abc.pqr.com", "pqr.yum.abc.com"]

Now, for each domain in the list I want to generate subdomains.

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

For example the subdomains of domain "abc.pqr.com" would be

["pqr.com", "abc.pqr.com"]

Also, for domain "pqr.yum.abc.com" the subdomains would be

["yum.abc.com", "pqr.yum.abc.com", "abc.com"]

So the out put of the method would be –

["yum.abc.com", "pqr.yum.abc.com", "abc.com", "pqr.com", "abc.pqr.com"]

>Solution :

First you have to iterate on elements then split your element by the ‘.’ seperator. After that in order to keep the ‘com’ element intact, we iterate on the range – 1. After creating every alternative, we join the result again with the seperator "."

domains = ["abc.pqr.com", "pqr.yum.abc.com"]
domains_new = []
for d in domains:
    liste = d.split(".")
    for i in range(len(liste)-1):
        domains_new.append(liste[i:])
domains_new = [".".join(ele) for ele in domains_new]
domains_new

output:

['abc.pqr.com', 'pqr.com', 'pqr.yum.abc.com', 'yum.abc.com', 'abc.com']
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