简体   繁体   中英

Merge two lists of objects containing lists

I have a directory tree containing html files called slides. Something like:

slides_root
|
|_slide-1
| |_slide-1.html
| |_slide-2.html
|
|_slide-2
|  |
|  |_slide-1
|  |  |_slide-1.html
|  |  |_slide-2.html
|  |  |_slide-3.html
|  |
|  |_slide-2
|    |_slide-1.html

...and so on. They could go even deeper. Now imagine I have to replace some slides in this structure by merging it with another tree which is a subset of this.

WITH AN EXAMPLE: say that I want to replace slide-1.html and slide-3.html inside "slides_root/slide-2/slide-1" merging "slides_root" with:

slide_to_change
|
|_slide-2
  |
  |_slide-1
   |_slide-1.html
   |_slide-3.html

I would merge "slide_to_change" into "slides_root". The structure is the same so everything goes fine. But I have to do it in a python object representation of this scheme.

So the two trees are represented by two instances - slides1, slides2 - of the same "Slide" class which is structured as follows:

Slide(object):

def __init__(self, path):
    self.path = path
    self.slides = [Slide(path)]

Both slide1 and slide2 contains a path and a list that contain other Slide objects with other path and list of Slide objects and so on.

The rule is that if the the relative path is the same then I would replace the slide object in slide1 with the one in slide2.

How can achieve this result? It is really difficult and I can see no way out. Ideally something like:

for slide_root in slide1.slides:
    for slide_dest in slide2.slides:
        if slide_root.path == slide_dest.path:
             slide_root = slide_dest
// now restart the loop at a deeper level
// repeat

Thank everyone for any answer.

Sounds not so complicated.

Just use a recursive function for walking the to-be-inserted tree and keep a hold on the corresponding place in the old tree.

If the parts match:
    If the parts are both leafs (html thingies):
        Insert (overwrite) the value.
    If the parts are both nodes (slides):
        Call yourself with the subslides (here's the recursion).

I know this is just kind of a hint, just kind of a sketch on how to do it. But maybe you want to start on this. In Python it could look sth like this (also not completely fleshed out):

def merge_slide(slide, old_slide):
  for sub_slide in slide.slides:
    sub_slide_position_in_old_slide = find_sub_slide_position_by_path(sub_slide.path)
    if sub_slide_position_in_old_slide >= 0:  # we found a match!
      sub_slide_in_old_slide = old_slide.slides[sub_slide_position_in_old_slide]
      if sub_slide.slides:  # this is a node!
        merge_slide(sub_slide, sub_slide_in_old_slide)  # here we recurse
      else:  # this is a leaf!  so we replace it:
        old_slide[sub_slide_position_in_old_slide] = sub_slide
    else:  # nothing like this in old_slide
      pass  # ignore (you might want to consider this case!)

Maybe that gives you an idea on how I would approach this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM