python - Merge two lists of objects containing lists -
i have directory tree containing html files called slides. 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 on. go deeper. imagine have replace slides in structure merging tree subset of this.
with example: want replace slide-1.html , 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 merge "slide_to_change" "slides_root". structure same goes fine. have in python object representation of scheme.
so 2 trees represented 2 instances - slides1, slides2 - of same "slide" class structured follows:
slide(object): def __init__(self, path): self.path = path self.slides = [slide(path)]
both slide1 , slide2 contains path , list contain other slide objects other path , list of slide objects , on.
the rule if the relative path same replace slide object in slide1 1 in slide2.
how can achieve result? difficult , can see no way out. ideally like:
for slide_root in slide1.slides: slide_dest in slide2.slides: if slide_root.path == slide_dest.path: slide_root = slide_dest // restart loop @ deeper level // repeat
thank answer.
sounds not complicated.
just use recursive function walking to-be-inserted tree , keep hold on corresponding place in old tree.
if parts match: if parts both leafs (html thingies): insert (overwrite) value. if parts both nodes (slides): call subslides (here's recursion).
i know kind of hint, kind of sketch on how it. maybe want start on this. in python sth (also not fleshed out):
def merge_slide(slide, old_slide): 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: # found match! sub_slide_in_old_slide = old_slide.slides[sub_slide_position_in_old_slide] if sub_slide.slides: # node! merge_slide(sub_slide, sub_slide_in_old_slide) # here recurse else: # leaf! replace it: old_slide[sub_slide_position_in_old_slide] = sub_slide else: # nothing in old_slide pass # ignore (you might want consider case!)
maybe gives idea on how approach this.
Comments
Post a Comment