Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9319417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:22:49+00:00 2026-06-19T03:22:49+00:00

I’ve got a set of data like this: data = { 1: {root: [2],

  • 0

I’ve got a set of data like this:

data = { 1: {"root": [2],
             "leaf": [10, 11, 12],
             },
         2: {"root": [1,3],
             "leaf": [13, 14, 15],
             },
         3: { "root": [2],
              "leaf": [16, 17],
            },
         4: {"root": [],
             "leaf": [17, 18, 19],
             },
         5: { "root": [],
              "leaf": [20, 21]
             },
       }

From this data, the initial key is a root node index, it contains a dictionary explaining which root nodes and leaf nodes are related to it.

I want to merge all indexes into related lists.

  • A root index connected by a root index, both/all root indexes and all leaf indexes are merged in the resulting list.
  • A root index may be connected to another root via a leaf, root indexes and all leaf indexes are merged in the resulting list.

I’m having a bit of trouble figuring out the best way to traverse and merge the data. From the above data set the Expected Output is:

[[1, 2, 3, 4, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [5, 20, 21]]

Fixed Attempt, seems to work, is there a more efficient method?

class MergeMachine(object):
    processed = []

    def merge(self, idx, parent_indexes, existing):
        if idx not in self.processed:
            parent_indexes.append(idx)
            if self.data[idx]["root"]:
                for related_root_idx in self.data[idx]["root"]:
                    if related_root_idx not in self.processed and related_root_idx not in parent_indexes:
                        existing.extend(self.merge(related_root_idx, parent_indexes, existing))
                        self.processed.append(related_root_idx)
            existing.append(idx)
            existing.extend(self.data[idx]["leaf"])
            self.processed.append(idx)
        return existing

    def process(self, data):
        results = []
        self.data = data
        for root_idx in self.data.keys():
            r = set(self.merge(root_idx, [], []))
            if r:
                combined = False
                for result_set in results:
                    if not r.isdisjoint(result_set):
                        result_set.union(r)
                        combined = True
                if not combined:
                    results.append(r)
        return results

mm = MergeMachine()
mm.process(data)

Is there a efficient way to merge the data into the expected output?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T03:22:50+00:00Added an answer on June 19, 2026 at 3:22 am

    I have no idea if this is efficient, but it seems to work:

    data = #your data as posted
    
    data = [set ( [k] ) | set (v ['root'] ) | set (v ['leaf'] ) for k, v in data.items () ]
    merged = []
    while data:
        e0 = data [0]
        for idx, e in enumerate (data [1:] ):
            if e0 & e:
                data [idx + 1] = e | e0 #idx is off by 1 as I enumerate data [1:]
                break
        else: merged.append (e0)
        data = data [1:]
    
    print (merged)
    

    I guess that in a worst case scenario (i.e. no possible merge) the cost should be O(n**2). And it is serial without recursion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I am using jsonparser to parse data and images obtained from json response. When
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.