2

I have a set of URLs like this:

http://example.com/one/two/three/four/
http://example.com/one/two/three/five/
http://example.com/one/two/three/five/six
http://example.com/one/two/five

And I need to create a tree-like dictionary that looks like this:

{
    'example.com': 
    {
        'one': 
        {
            'two':
            {
                'three':
                {
                    'four': None,
                    'five': 
                    {
                        'six': None
                    }
                },
                'five': None
            }
        }
    }
}

The URLs can have any number of levels, so the depth of the dictionary is not known beforehand.

How can I traverse over this dictionary to add the keys?

My current pseudo-code is:

list_of_links = [
    'http://example.com/one/two/three/four/',
    'http://example.com/one/two/three/five/',
    'http://example.com/one/two/three/five/six',
    'http://example.com/one/two/five',
]

dic = {'example.com':None}

for link in list_of_links: 
    url_path = urlparse.urlparse(link).path
    url_path_parts = url_path.split('/')

    curr_dic_path = 'example.com'
    for segment in url_path_parts:

        # How to do this?
        if segment in dic's current path:
            curr_dic_path = curr_dic_path + segment
        else
            add_segment_to_dic_current_path

Or is there a better way to do this? I have been trying for the past two days and could not get it done.

I've already searched through similar questions such as this one but they don't answer my question.

  • Where would you want to add the keys? What would you expect the outcome to be? – mrCarnivore Jan 04 '18 at 12:09
  • @mrCarnivore: I want to add the keys in the dictionary itself based on the URL I have. Which could have N-number of segments. I have added the outcome to my question itself. – UnderpoweredNinja Jan 04 '18 at 12:12
  • @cᴏʟᴅsᴘᴇᴇᴅ: Okay, please explain why you closed the question as a duplicate. It clearly does not answer my question. – UnderpoweredNinja Jan 04 '18 at 12:12
  • 1
    Um, your question is a textbook TRIE question, just modify the code in the answer and you're good to go. – cs95 Jan 04 '18 at 12:13
  • 1
    @cᴏʟᴅsᴘᴇᴇᴅ **This question was marked as an exact duplicate of an existing question.** -- This question is more about traversing the dictionary. Use a TRIE would have been more appropriate as a comment. It's in no way an exact duplicate. Could you explain why you think otherwise? – UnderpoweredNinja Jan 04 '18 at 12:18
  • 1
    If an answer addresses another question, then, in my opinion, is a suitable duplicate target. If you disagree, you can either 1. Appeal to another badge holder, or 2. Contest the closure on meta. – cs95 Jan 04 '18 at 12:20
  • 4
    It seems to me that the accepted answer on the linked duplicate is really what you need. The only differences are that your `_end` is `None`, and that instead of looping over each character in a word you have to loop over every item in `url_path_parts`, or something similar (cc @cᴏʟᴅsᴘᴇᴇᴅ). – Andras Deak -- Слава Україні Jan 04 '18 at 12:29
  • 3
    Discussed on [meta](https://meta.stackoverflow.com/questions/361421/understanding-why-my-question-was-closed-so-i-can-ask-better-questions-in-the-fu) – BDL Jan 04 '18 at 12:35

0 Answers0