0

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]
10
  • Are you tying to construct the dictionary with the two lists? Commented Feb 14, 2015 at 0:05
  • Please focus on what are you trying to achieve (what is the goal of all this) it's more important than how you're trying to do it. Commented Feb 14, 2015 at 0:06
  • @MalikBrahimi List 2 is a list of all domains/subdomains and is used for checking the strings in List1. List 2 also is the key for the dictionary, so any match will be appended to a list with that domain in list2 as the key. Does that answer your question? Commented Feb 14, 2015 at 0:07
  • @alfasin I'm trying to organize a list of hosts by their domains. Commented Feb 14, 2015 at 0:08
  • Make a list of domains and corresponding subdomains, don't put them together. Commented Feb 14, 2015 at 0:09

2 Answers 2

1

Use conditionals:

list1 = [
  'host1.domain.com',
  'host2.domain.com',
  'host20.sub.domain.com',
  'host31.sub.domain.com',
  'host1.example.com',
  'host1.sub.example.com'
]

list2 = [
    'domain',
    'example'
]

list3 = [
  'sub.domain',
  'sub.example'
]

my_dict = {i:[] for i in list2 + list3}

for i in list1:
    for j in zip(list2, list3):
        if j[1] in i:
            my_dict[j[1]].append(i)

        elif j[0] in i:
            my_dict[j[0]].append(i)
Sign up to request clarification or add additional context in comments.

Comments

1

Here's how I would do it (after a billion edits):

hosts = [
  'host1.domain.com',
  'host2.domain.com',
  'host20.sub.domain.com',
  'host31.sub.domain.com',
  'host1.example.com',
  'host1.sub.example.com'
]

domains = [
  'domain',
  'sub.domain',
  'example',
  'sub.example'
]

import re
import pprint

dot = r'.'
anything_but_dot = r'[^.]*'
prefix = anything_but_dot + dot

answer = {}
for domain in domains:
    compiled = re.compile(prefix + domain)
    answer[domain] = []
    for host in hosts:
        if compiled.match(host):
            answer[domain].append(host)

pprint.pprint(answer)

this gets the result:

{'domain': ['host1.domain.com', 'host2.domain.com'],
 'example': ['host1.example.com'],
 'sub.domain': ['host20.sub.domain.com', 'host31.sub.domain.com'],
 'sub.example': ['host1.sub.example.com']}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.