将`GatherBy`转换为不同的列表

Yar*_*tov 7 wolfram-mathematica

我有listAlistB相同的大小.我做GatherBylistA,这是重排列表.应用相同重排的优雅方法是什么listB

例如

listA = {1, 2, 3};
listB = {a, b, c};
listA1 = GatherBy[{1, 2, 3}, OddQ];
Run Code Online (Sandbox Code Playgroud)

listB1 应该成为 {{a, c}, {b}}

更新 感谢有趣的想法,我最终做了类似于belisarius的事情.这让我想起了Python的"decorate-sort-undecorate"模式

decorated = Thread[{listA, listB}];
grouped = GatherBy[decorated, OddQ[First[#]] &];
listB1 = Map[Last, grouped, {2}]
Run Code Online (Sandbox Code Playgroud)

Dr.*_*ius 4

好吧,第一次第二次尝试:

(警告警告......“优雅”是一个完全主观的概念)

gBoth[lslave_, lmaster_, f_] := 
                 {Part[#, All, All, 1], Part[#, All, All, 2]} &@ 
                 GatherBy[Transpose[{lslave, lmaster}], f[#[[2]]] &]

lmaster = {1, 2, 3};
lslave = {a, b, c};  

{lslave1, lmaster1} = gBoth[lslave, lmaster, OddQ]  
Run Code Online (Sandbox Code Playgroud)

出去

{{{a, c}, {b}}, {{1, 3}, {2}}}  
Run Code Online (Sandbox Code Playgroud)

编辑

请注意,要运行此代码,您必须具有

 Dimensions[lslave][[1;;Length[Dimensions@lmaster]]] == Dimensions@lmaster  
Run Code Online (Sandbox Code Playgroud)

但两个列表的更深层次的内部结构可能不同。例如:

lmaster = {{1, 2, 3}, {2, 3, 4}};
lslave = {{{a}, {b}, {c}}, {{a}, {b}, {c}}};

{lslave1, lmaster1} = gBoth[lslave, lmaster, #[[1]] < 3 &]
Run Code Online (Sandbox Code Playgroud)

出去

{{{{{a}, {b}, {c}}, {{a}, {b}, {c}}}}, {{{1, 2, 3}, {2, 3, 4}}}}
Run Code Online (Sandbox Code Playgroud)

哈!