I have a list of hosts that are part of multiple domains and subdomains. I'm trying to convert the list to a dict:list so the hosts are organized by domain/subdomain.
Pythons 'in' string matching would match all subdomains and domain. I was trying /(?!sub).domain/ as my regex but that doesn't seem to be matching correctly.
Trying to translate List1 to Dict based on List2
# A list of every host
host_list = [
'host1.domain.com',
'host2.domain.com',
'host20.sub.domain.com',
'host31.sub.domain.com',
'host1.example.com',
'host1.sub.example.com'
]
# A list of all domains we want to organize in the dictionary
domain_list = [
'two.sub.domain',
'sub.example',
'sub.domain',
'domain',
'example'
]
Desired results
domain_dict = {
'domain': ['host1.domain.com', 'host2.domain.com'],
'sub.domain': ['host20.sub.domain.com', 'host31.sub.domain.com'],
'example': ['host1.sub.example.com'],
'sub.example': ['host1.sub.example.com']
}
Solution where we still have one list of domains and support multiple subdomains.
One caveat about this is the list of domains need to start from the deepest (most specific) subdomain. See the domain_list order has sub.domain before domain.
# We want to protect the original host list
host_list_copy = list(host_list)
for domain in domain_list:
# Get only the hosts that are part of the same subdomain/domain
temp_host_list = [x for x in host_list_copy if (domain in x)]
# Add the list to the dictionary
domain_dict[domain] = temp_host_list
# Remove the temp_host_list records from the original host_list_copy
host_list_copy[:] = [x for x in host_list_copy if x not in temp_host_list]