Gather on first list, apply to second list
I have two lists. The first list is L1={z,x,y,x,x,y} and the second list is L2={a,b,c,d,e,f} in which all elements are distinct. I want to gather the first list to get {{x,x,x},{y,y},{z}} and then apply it to the second list to get {{b,d,e},{c,f},{a}}. Can this be done easily?
Given,
This gets you the partitioning:
I assume that your expected result is sorted by size (descending), so that would be:
Given the sorting ambiguity mentioned by @march, maybe you didn't want to sort on size but alphabetically based on the items in L1. If so...
It turns out that the result is the same in each case.
There are many options. Here are a few:
all yield
If order doesn't matter, remove the Sorting parts of the code.
Yet another possibility:
{{{x, x, x}, {y, y}, {z}}, {{b, d, e}, {c, f}, {a}}}
{{x, x, x}, {y, y}, {z}}
{{b, d, e}, {c, f}, {a}}
{{a}, {b, d, e}, {c, f}}
{{b, d, e}, {c, f}, {a}}
I apologize for not completely specifying what is important. Given the two lists
L1 = {z, x, w, x, x, w};
L2 = {a, b, c, d, e, f};
I want L1 partitioned into equivalence classes in no particular order, with L2 being partitioned the same way:
Lv = L2[[#]] & /@ Values[PositionIndex[L1]]
gives Lv as {{a}, {b, d, e}, {c, f}} which is perfectly fine, as long as I can the associate the elements of L1 which correspond to each element of L2, such as:
Lk = Keys[PositionIndex[L1]]
which gives Lk as {z,x,w}.
I chose this method only because it is more intuitive (to me). Thank you all for your help, I need to learn more about associations.