Merge list with data in common

Hi Guys

Here is probably a simple problem but can’t solve it for the moment
link to the file :

classement-branches.gh (5.4 KB)

I have one list with many path
I want to merge all the path who have the last number in common with the first number of an other list
here is what I want to do (highligthed in blue)

I did try with the equal tool but I’m not able to do it…

Any Help ? Thanks !

Can you upload your data?
(having to NOT create a similar structured tree make it easyer for someone to be tempted to reply)

Also:
{0} have 0, 2, 10
{10} have 10, 18
Wouldn’t merging those two paths give> 0, 2, 10, 10, 18 (double 10) ???
You also want to remove duplicates?

edit with file to upload !

thanks riccardo
delete consecutive number will no be a problem.

C# script.
This is a “directly translated” algorithm, as like as doing it manually.
It can’t work if you have empty branches or your tree structure is not consecutive.
(ex {0},{1},{2},{4},{5} … missing {3})

You have to test it.
classement-branches_re.gh (14.7 KB)

private void RunScript(DataTree<int> x, bool y, bool z, ref object A)
  {
    int branches = x.BranchCount;
    Grasshopper.DataTree<int> tree = new Grasshopper.DataTree<int>();
    tree.AddRange(x.Branch(0), x.Path(0));
    bool found;
    for (int i = 1;i < x.BranchCount;i++){ // Starting from second branch as first is already passed
      found = false;
      for (int j = 0;j < tree.BranchCount;j++){
        int a = x.Branch(i)[0]; // First item of branch "i" of original x tree
        int b = tree.Branch(j)[tree.Branch(j).Count - 1]; // Last item of branch "j" of tree that is building up
        if (a == b){
          if (y){ // Removing consecutive duplicate by removing last item of current "j" branch
            tree.Branch(j).RemoveAt(tree.Branch(j).Count - 1); }
          tree.AddRange(x.Branch(i), new Grasshopper.Kernel.Data.GH_Path(j));
          found = true; // Found a branch in tree where to add x{i}
          if (z){ break;} // Exiting current for cycle if z=true
        }
      }
      if (!found){
        tree.AddRange(x.Branch(i), new Grasshopper.Kernel.Data.GH_Path(tree.BranchCount));
      }
    }
    A = tree;
  }
1 Like

Check if this is helpful as well with vanilla GH…

classement-branches_re.gh (14.9 KB)

1 Like

hi guys, thanks for your help, but it appear that your suggestion doesn’t work perfectly when I merge it on my grasshopper with the different files I’am working on and I don’t understan really why…
I did try to simplify my own work to turn it easier to work on…
so i’am no able to have only 2 value to work on on my tree…
hope it help you to help me…

classement-branches-V2.gh (10.2 KB)


thanks you so much !

I don’t understand what you mean…

classement-branches-V2_re.gh (14.2 KB)

basicly I want the code to go on and on as long as there common value in the list
when I repeat Riccardo’s tool 4/5 or 6 time I get the final result I want, the problem is that depending the file I ope, I don’t kno how many time I have to repeat the same code, so I want it to do it by itself…

Hs-Kim tool do a good work too, but it did creating many times the same list, so I would like the repeated list to be delete.

design_branche_44.gh (737.1 KB)

thanks you very much for helping

Oh my god!
I lost myself in your definition!

Please don’t do this, you should really work with smaller/simpler definitions. This could and will go against you.
Clean your code, optimize, make clusters.

Also I didn’t have like 10 of your plugins, so everything is without data for me…

What you need to do?
Reiterate in the list until it doesn’t “shrink” anymore?

1 Like

It’s not difficult to iterate this using anemone loop…

classement-branches-V2_reV2.gh (20.7 KB)

thank you very much to both of you.

““Reiterate in the list until it doesn’t “shrink” anymore?”” -> Yes

Riccardo, I know that I shouldn’t use many time your C++ tool connected together, but I did try because it appear that your code do not seem to always restart from the top.
But I don’t do C++ code so I don’t know how to modify it…
here is a file with some more explanation and example, I know that my question is hard to explain but in fact I think it’s a simple one for tough grasshopper guys as you seems to be.

classement-branches-V4.gh (20.7 KB)

please ask if you don’t understand, I really need to solve this case !
thank you so much !

Yes, old “repeat from top” boolean was flawed… useless.

Have a look at this:
classement-branches-V5.gh (6.4 KB)

Hope it doesn’t have errors…

If you want to always set “true” the loop from start thing, remove the z input param and edit code and add a line:
bool z = true;
at the start of the script, just under “private void RunScript”…
And if you need more looping, increase value of “maxloops” inside the code…

Cya!

thank you very much !

it seems to work very well, but just after a small modification, I did a first “shrink” then, reverse the list and “shrink” again.

It’s now seems to work like I wish !

thank you so much guy’s !