I have two lists with multiple array elements:
myList = {{myArray[1, 0, 0], myArray[1, 0, 1], myArray[1, 0, 2], myArray[1, 1, 0], myArray[1, 1, 1], myArray[1, 1, 2], myArray[1, 2, 0], myArray[1, 2, 1], myArray[1, 2, 2]}, {myArray[2, 0, 0], myArray[2, 0, 1], myArray[2, 0, 2], myArray[2, 1, 0], myArray[2, 1, 1], myArray[2, 1, 2], myArray[2, 2, 0], myArray[2, 2, 1], myArray[2, 2, 2]}}
I want to get a combination of the two lists that have all of the first with the same third element and all of the second with a different second element. So for this data input, my desired output is the following:
desiredResult={{myArray[1, 0, 0], myArray[1, 1, 0], myArray[1, 2, 0], myArray[2, 1, 0], myArray[2, 1, 1], myArray[2, 1, 2], myArray[2, 2, 0], myArray[2, 2, 1], myArray[2, 2, 2]}, {myArray[1, 0, 1], myArray[1, 1, 1], myArray[1, 2, 1], myArray[2, 0, 0], myArray[2, 0, 1], myArray[2, 0, 2], myArray[2, 2, 0], myArray[2, 2, 1], myArray[2, 2, 2]}, {myArray[1, 0, 2], myArray[1, 1, 2], myArray[1, 2, 2], myArray[2, 0, 0], myArray[2, 0, 1], myArray[2, 0, 2], myArray[2, 1, 0], myArray[2, 1, 1], myArray[2, 1, 2]}}
I have come up with a way to do this but am wondering if there is a way to have "rules within rules" that would do it more succinctly.
PickExclusiveSets[rowPair_] := Block[{firstRow = rowPair[[1]],secondRow = rowPair[[2]],firstRowByEnd,secondRowByStart},firstRowByEnd = GatherBy[firstRow, #[[3]] &];secondRowByStart = GatherBy[secondRow, #[[2]] &];With[{firstBlockEnd = #[[1, -1]]},Flatten[{#, Select[secondRowByStart, #[[1, 2]] != firstBlockEnd &]}]] & /@ firstRowByEnd];
Side point, is there a way to have code formatted to look better within a code block in a question?
Did some poking around and I think I figured out how to have code better formatted in a post. Such as this:
PickExclusiveSets[rowPair_] :=
Block[ {
firstRow = rowPair[[1]],
secondRow = rowPair[[2]],
firstRowByEnd,
secondRowByStart
},
firstRowByEnd = GatherBy[firstRow, #[[3]] &];
secondRowByStart = GatherBy[secondRow, #[[2]] &];
With[ {firstBlockEnd = #[[1, -1]]},
Flatten[{#,
Select[secondRowByStart, #[[1, 2]] != firstBlockEnd &]}]
] & /@ firstRowByEnd
];
In my earlier pasting, I didn't have the required indents. Had to paste from a notebook into another text editor and then copy-paste back here.